aspose file tools*
The moose likes Beginning Java and the fly likes Bad Double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Bad Double" Watch "Bad Double" New topic
Author

Bad Double

Andy Rayner
Ranch Hand

Joined: Dec 09, 2004
Posts: 36
Hello, I am having some problems with a double, is it just me but has anyone noticed when you add 0.8 and 0.9 together, instead of getting 1.7 you get 1.700000020 which comes as a problem when im trying to make it a cost
The price in my burger bar is now
Regular Burger �1.700000020 hehe
Can anyone help me with this.
Thank You
Jeff Allison
Greenhorn

Joined: Dec 09, 2004
Posts: 20
If you are dealing with money you can just use ints and treat the values as pennies. So 100 = 1 dollar (or one pound). Then format on output. This avoids all the rounding troubles.
Andy Rayner
Ranch Hand

Joined: Dec 09, 2004
Posts: 36
i would do, but the problem is i have to stick to a specification
Most of the items are �1.40 or �0.80.
Is there any reason why im getting all these remainders? Can i not just have an output of 0.00 cut the rest off?
Thanks again for the help
Jeff Allison
Greenhorn

Joined: Dec 09, 2004
Posts: 20
i would do, but the problem is i have to stick to a specification
Most of the items are �1.40 or �0.80.


Well �1.40 would be 140 and �0.80 would be 80. Add them together and you get 220. Formatted for output that's �2.20. You really can use integers here and just scale by 100.

If you must use doubles, then yes, you can truncate. The reason you're getting rounding errors is that decimal numbers don't always fit precisely into the binary representations used to represent them as floating point numbers.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Try this:



Output: Please pay �1.70

This rounds to your local currency's decimal places.
[ December 09, 2004: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
Andy Rayner
Ranch Hand

Joined: Dec 09, 2004
Posts: 36
Thanks i'll try that
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
By way of explanation, let's look at base ten arithmetic that you should have learned in grade school (or whatever you guys call it over in England ). What is 1 / 3? You might say it's 0.3333, but that is only rounded to four decimal places and no longer the exact value of one-third. Binary arithmetic used by a computer suffers from similar problems. Fractional values are subject to round off error when stored in a binary format. If you want more technical details about this, you should google for "IEEE floating point numbers" or something similar. IEEE made the standard that most computers use for storing floating point numbers. There should be plenty of information on the Net about the particular problem you have encountered here. Also, this topic has been discussed previously here, so you may want to use the Search tool to see what has been said about it. If you don't find anything on this particular forum, you may want to try searching "Java In General (intermediate)".

HTH

Layne


Java API Documentation
The Java Tutorial
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
As a rule never use double to represent currency.
Decimal values can't be represented exactly by float or double.
The solution is to use int, long or BigDecimal
[ December 10, 2004: Message edited by: Nigel Browne ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

In the following Sun article, scroll down about half way to the section, "Some Things You Should Know About-Floating Point Arithmetic."

http://java.sun.com/developer/JDCTechTips/2003/tt0204.html

(Also read the API for the BigDecimal constructor that takes a double as an argument.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
jQuery in Action, 2nd edition
 
subject: Bad Double