aspose file tools*
The moose likes Beginning Java and the fly likes Problem with Double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with Double" Watch "Problem with Double" New topic
Author

Problem with Double

changu mani
Ranch Hand

Joined: Aug 31, 2012
Posts: 30
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: 11241
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7676
    
  19

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?
Articles by Winston can be found here
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61063
    
  66

You might want to investigate the BigDecimal class.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
changu mani
Ranch Hand

Joined: Aug 31, 2012
Posts: 30
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: 2248
    
  47
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: 11241
    
  16

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: 2248
    
  47
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
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
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: 61063
    
  66

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: 18541
    
    8

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
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
> The statement "..." is true.

Did I say it was not? Methinks not.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with Double