function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rajsrajs 

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

Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_

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