Float and double are floating binary point types. In other words, they represent a number like this:
The binary number and the location of the binary point are both encoded within the value.
Decimal is a floating decimal point type. In other words, they represent a number like this:
Again, the number and the location of the decimal point are both encoded within the value – that’s what makes decimal still a floating-point type instead of a fixed point type.
The important thing to note is that humans are used to representing non-integers in a decimal form, and expect exact results in decimal representations; not all decimal numbers are exactly representable in binary floating-point – 0.1, for example – so if you use a binary floating-point value you’ll actually get an approximation to 0.1. You’ll still get approximations when using a floating decimal point as well – the result of dividing 1 by 3 can’t be exactly represented, for example.
As for what to use when:
For values which are “naturally exact decimals”, it’s good to use decimal. This is usually suitable for any concepts invented by humans: financial values are the most obvious example, but there are others too. Consider the score given to divers or ice skaters, for example.
For values which are more artifacts of nature which can’t really be measured exactly anyway, float/double are more appropriate. For example, scientific data would usually be represented in this form. Here, the original values won’t be “decimally accurate” to start with, so it’s not important for the expected results to maintain the “decimal accuracy”. Floating binary point types are much faster to work with than decimals.
- 1Bitbucket and Visual Studio
- 1How to dump and restore database in MongoDB
- 1How to compress and extract files/folders using the tar command on Ubuntu
- 1How To Create a New User and Grant Permissions in MySQL
- 2Abstract and Virtual Methods
- 0What is the difference between custom dimensions and metrics in Google Analytics
- 5How can I rename git local and remote branch name?
- 1Events and delegates in C#
- 1how to write function to generate an array between two integers
- 1*ngIf and *ngFor on same element causing error
- 1How to make my footer cover 100% height and width of the page from top?