You need to sign in to do that
Don't have an account?
rajs
Decimal places are incorrect in Double Data Type calculations
Hello,
I have used 'Double' data type variables in my simple addition calculations. Input valuses containing 2 decimal places so I'm expecting same decimal places in the result of addition but it's not giving the expected result.
Double du1 = 600.66, du2 = 600.00; Double du3 = du1+du2; System.debug('**** Total = '+ du3); Here Expected Result = 1200.66 but Actual Result = 1200.6599999999999
If I use Decimal data type, I can get the expected result. Whats wrong in 'Double' datatype. Can any one help me to understand the issue
Note: I'm getting these unexpected result for some selected inputs only
Thanks,
Raj
This is actually a common issue in programming languages. It's because some numbers are impossible to represent with machine code.
One common number that has this issue is '0.1'. Computers actually can't exactly represent this number. The link below takes you to an answer to a separate answer, but the reason is the same.
http://stackoverflow.com/questions/2925223/floating-point-arithmetic-modulo-operator-on-double-type