• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Problem with Double

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13082
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to investigate the BigDecimal class.
 
changu mani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13082
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I would expand that to say you should never use a floating point type to store something that has discrete values.

A fair point.

 
Rancher
Posts: 1043
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 27460
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1043
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> The statement "..." is true.

Did I say it was not? Methinks not.
 
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic