float (the C# alias for System.Single) and double (the C# alias for System.Double) are floating binary point types. float is 32-bit; double is 64-bit. In other words, they represent a number like this:
10001.10010110011
The binary number and the location of the binary point are both encoded within the value.
decimal (the C# alias for System.Decimal) is a floating decimal point type. In other words, they represent a number like this:
12345.65789
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 artefacts of nature which can't really be measured exactly anyway,
float/doubleare 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.
float (the C# alias for System.Single) and double (the C# alias for System.Double) are floating binary point types. float is 32-bit; double is 64-bit. In other words, they represent a number like this:
10001.10010110011
The binary number and the location of the binary point are both encoded within the value.
decimal (the C# alias for System.Decimal) is a floating decimal point type. In other words, they represent a number like this:
12345.65789
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 artefacts of nature which can't really be measured exactly anyway,
float/doubleare 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.
Precision is the main difference.
Float - 7 digits (32 bit)
Double-15-16 digits (64 bit)
Decimal -28-29 significant digits (128 bit)
Decimals have much higher precision and are usually used within financial applications that require a high degree of accuracy. Decimals are much slower (up to 20X times in some tests) than a double/float.
Decimals and Floats/Doubles cannot be compared without a cast whereas Floats and Doubles can. Decimals also allow the encoding or trailing zeros.
float flt = 1F/3;
double dbl = 1D/3;
decimal dcm = 1M/3;
Console.WriteLine("float: {0} double: {1} decimal: {2}", flt, dbl, dcm);
Result :
float: 0.3333333
double: 0.333333333333333
decimal: 0.3333333333333333333333333333
Can somebody, in simple terms, explain the difference between the three datatypes in SQL? Thank you
mysql - What's difference between float, double and decimal in sql? - Stack Overflow
Decimal or double? - Salesforce Stack Exchange
mysql - Difference between float and decimal data type - Stack Overflow
int vs float vs double vs decimal
Amounts of memory allocated per variable.
Computational ease.
Computational speed.
More on reddit.comRegarding to the official documentation, we have the next diferences:
- FLOAT - Floating-Point Types (Approximate Value), uses four bytes
- DOUBLE - Floating-Point Types (Approximate Value), uses eight bytes
- DECIMAL - Fixed-Point Types (Exact Value)
You can ask about differences between floating and fixed datatypes, but this is not so easy to describe, try to read the same official documentation, or this article, for example.
Computer scientist Brian Kernighan wrote:
10.0 times 0.1 is hardly ever 1.0.
FLOAT and DOUBLE are both IEEE 754 format values. They store a wide range of floating-point value, including very small or very large values, but they have the disadvantage that they have rounding errors.
For examples and details on that, read:
- https://dev.mysql.com/doc/refman/8.0/en/problems-with-float.html
- https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
FLOAT stores 32 bits and DOUBLE stores 64 bits. This just means you can store more significant digits with a DOUBLE, but it still has the same rounding behavior.
These data types are good for scientific measurements like temperature, where the difference between 39 degrees and 38.999999997 degrees won't make a significant difference, especially if you're only doing things like averages or standard deviations.
Because of the rounding behavior, FLOAT and DOUBLE should never be used to store values of currency.
For currency values (e.g. the salaries you need to store), use DECIMAL.
DECIMAL and NUMERIC are exact scaled numerics. You define a column with a specific number of digits, and a specific number of digits to the right of the decimal point. There are no rounding problems; values you store come right back out exactly as you stored them. You can search the data for specific values.
DECIMAL and NUMERIC are synonyms. There's no difference. In fact, if you use NUMERIC as the type, MySQL silently replaces it with DECIMAL.
You may also like to read the chapter "Rounding Errors" in my book, SQL Antipatterns, Volume 1: Avoiding the Pitfalls of Database Programming.
Biggest difference is the Double is a 64-bit and Decimal is not..... In addition:
Scientific notation (e) for Doubles is not supported.
Also, currency fields in salesforce are Decimals. The scale (number of digits to the right of decimal point) can be set for a Decimal, but cannot for a Double. This is important for currency and other calculations (division). Doubles do not have this option. You'll find that Doubles have a limited number of methods available to them compared to Decimals. Unless you know that you're dealing with very large numbers requiring a high degree of precision, Decimals will generally be a better choice.
Here is a good discussion on the issue from people way more experienced in the ins and outs than me:
From Peter Knoll:
According to the primitive data type docs currency fields are automatically assigned the type Decimal. Double is 64-bit. I'm not sure what Decimal is behind the scenes. It might be BigDecimal like or something else
From Rich Unger (Regarding the type of Decimal a decimal is:
For example, with Java you should never use floating point representations to handle monetary calculations, but instead use BigDecimal, and, as you stated you need to be careful with the comparisons. The same goes for Apex with respect to Double
SF Decimal is a BigDecimal
Run the following in Exec Anon and you'll see what I mean.
Double zeroPointZeroOne = 0.01; Double sum = 0.0; for (Integer i = 0; i < 10; i++) { sum += zeroPointZeroOne; } // 0.09999999999999999 System.debug('Double sum=' + sum); Decimal zeroPointZeroOneD = 0.01; Decimal sumD = 0.0; for (Integer i = 0; i < 10; i++) { sumD += zeroPointZeroOneD; } // 0.1 System.debug('Decimal sumD=' + sumD);
Comparing floating-point values
Its important to note that in the above Decimal example, in declaring Decimal sumD = 0.0;, the scale was set to 1, meaning 0.099 would be rounded up to 0.1 and 0.01 would be rounded down to 0.0. The default rounding would round 0.05 down to 0.0.
It depends what the number represents. If the number represents currency then Decimal is the way to go. In a Decimal every digit that you see is stored exactly as you see it and the number of decimal places and rounding modes are tightly defined. This avoids indeterminate results when large numbers are involved in intermediate or final values. Financial algorithms typically require that sort of control.
The strength of float/double is in representing numbers that vary hugely in size - 10 to the power of +/-38 for float and 10 to the power of +/-308 for double. Also there is direct hardware support for calculations on those making the calculation many orders of magnitude faster. But that only matters if running complex algorithms that perform many thousands of operations. The weakness is that the number of bits used to represent the value is limited and rounding errors can creep in surprisingly easily.
Unless you are doing complex scientific calculations Decimal is usually the best way to go.
This is what I found when I had this doubt.
Use with AImysql> create table numbers (a decimal(10,2), b float);
mysql> insert into numbers values (100, 100);
mysql> select @a := (a/3), @b := (b/3), @a * 3, @b * 3 from numbers \G
*************************** 1. row ***************************
@a := (a/3): 33.333333333
@b := (b/3): 33.333333333333
@a + @a + @a: 99.999999999000000000000000000000
@b + @b + @b: 100
The decimal did exactly what's supposed to do on this cases, it truncated the rest, thus losing the 1/3 part.
So for sums the decimal is better, but for divisions the float is better, up to some point, of course. I mean, using DECIMAL will not give you a "fail proof arithmetic" in any means.
A "float" in most environments is a binary floating-point type. It can accurately store base-2 values (to a certain point), but cannot accurately store many base-10 (decimal) values. Floats are most appropriate for scientific calculations. They're not appropriate for most business-oriented math, and inappropriate use of floats will bite you. Many decimal values can't be exactly represented in base-2. 0.1 can't, for instance, and so you see strange results like 1.0 - 0.1 = 0.8999999.
Decimals store base-10 numbers. Decimal is an good type for most business math (but any built-in "money" type is more appropriate for financial calculations), where the range of values exceeds that provided by integer types, and fractional values are needed. Decimals, as the name implies, are designed for base-10 numbers - they can accurately store decimal values (again, to a certain point).