| Author |
Problem with Double
|
changu mani
Greenhorn
Joined: Aug 31, 2012
Posts: 25
|
|
Dear All,
I've this code.
It prints 866.0799999999999.
If I change d2 to 123.02 it gives the output as 866.05. Please let me know if this is called rounding error in java. If this topic has already been discussed , please give me the link. I'll take a look and understand.
Thanks in Advance.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9955
|
|
this has nothing to do with java. it has to do with how all computers store a number. floating point math is never precise. If you search this forum, you will find many topics on this subject.
Basically, the rule is that if you need accuracy, don't use doubles.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4761
|
|
changu mani wrote:If this topic has already been discussed , please give me the link. I'll take a look and understand.
It has , and this link is so well known now, it's just called "Goldberg".
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56215
|
|
|
You might want to investigate the BigDecimal class.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
changu mani
Greenhorn
Joined: Aug 31, 2012
Posts: 25
|
|
|
Thank you All. In our Banking Product, they have made use of Double in upload Payroll files module. Now the total Debit amount is not equal to total credit amount for a few scenarios. It created all the problem. Thanks again for the links.
|
 |
Tony Docherty
Bartender
Joined: Aug 07, 2007
Posts: 1173
|
|
Thank you All. In our Banking Product, they have made use of Double in upload Payroll files module.
You should never use floating point numbers to store financial values. You should always use integer types or the BigDecimal class.
See this article http://www.javapractices.com/topic/TopicAction.do?Id=13 on how to use BigDecimal.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9955
|
|
Tony Docherty wrote:You should never use floating point numbers to store financial values. You should always use integer types...
I would expand that to say you should never use a floating point type to store something that has discrete values. You wouldn't use a float to store the day of the month. You wouldn't use a float to store the number of students in a classroom. Since money is in discrete amounts, you should use some discrete variable type - like an int. This requires you to store values in pennies rather than dollars (or whatever currency equivalent you have), but that is trivial.
|
 |
Tony Docherty
Bartender
Joined: Aug 07, 2007
Posts: 1173
|
|
I would expand that to say you should never use a floating point type to store something that has discrete values.
A fair point.
|
 |
Ivan Jozsef Balazs
Ranch Hand
Joined: May 22, 2012
Posts: 380
|
|
you should never use a floating point type to store something that has discrete values.
Some nitpicking: a floating point type has a value range consisting of discrete values, albeit not evenly distributed ones.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56215
|
|
Ivan Jozsef Balazs wrote:
you should never use a floating point type to store something that has discrete values.
Some nitpicking: a floating point type has a value range consisting of discrete values, albeit not evenly distributed ones.
That's not a nitpick -- it's a non sequitor.
The statement "you should never use a floating point type to store something that has discrete values." is true. It did not say that there are no discrete values possible.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
It's true, floating point numbers do constitute a list of discrete values. However this list is extremely unlikely to correspond to the list in anybody's real-life business scenario.
|
 |
Ivan Jozsef Balazs
Ranch Hand
Joined: May 22, 2012
Posts: 380
|
|
> The statement "..." is true.
Did I say it was not? Methinks not.
|
 |
 |
|
|
subject: Problem with Double
|
|
|