This discussion is archived
2 Replies Latest reply: Oct 2, 2012 11:59 AM by 730038

# Double precision problem with sum of double numbers

Currently Being Moderated
Hi, Everyone

Why Java rounded the sum the values (296931.85 + 21363.85) ? if the Double data type support the result 318295.7

--Operations

double varBalanceRecord = 0;
varBalanceRecord = 296931.85 + 21363.85;
System.out.println(varBalanceRecord);
varBalanceRecord = 296931.85D + 21363.85D;
System.out.println(varBalanceRecord);

varBalanceRecord = 318295.7;
System.out.println(varBalanceRecord);
varBalanceRecord = 318295.7D;
System.out.println(varBalanceRecord);

--Results

318295.69999999995
318295.69999999995
318295.7
318295.7

Edited by: Elix545 on Oct 2, 2012 11:33 AM
• ###### 1. Re: Double precision problem with sum of double numbers
Currently Being Moderated
• ###### 2. Re: Double precision problem with sum of double numbers
Currently Being Moderated
I found useful information about floating point datatype and the BigDecimal Class:

[http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html] , This include some recommendation for handled currency and big Numbers in java.

[http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html]

[http://www.javaranch.com/journal/2003/07/MoneyInJava.html]

[http://java.dzone.com/articles/working-money-java]

Thank you very much, sabre150.

#### Legend

• Correct Answers - 10 points