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

Double precision problem with sum of double numbers

730038 Newbie
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

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points