The Artima Developer Community
Sponsored Link

Java Answers Forum
Rounding error with double

2 replies on 1 page. Most recent reply: Nov 23, 2004 12:51 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Gibran Shah

Posts: 27
Nickname: gibby
Registered: Jun, 2004

Rounding error with double Posted: Nov 21, 2004 4:10 PM
Reply to this message Reply
Advertisement
I've got a problem with multiplying a double by 100.0. Soemtimes it works, other times it gives me rounding problems. Specifically, when the original value was 80000.01, multiplying it by 100.0 (d = 80000.01; d *= 100.0;) gave me 80000.9999999 instead of 80001.0. How can I prevent this?

Gib


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Rounding error with double Posted: Nov 22, 2004 3:37 AM
Reply to this message Reply
You can't.

Since computer floating data types are based on 2, not every number can be stored correctly.
a double value is stored in the following way:
a * b^c
a is between 0 and 1, all variables are based on 2

You could use the data type Decimal, wich stores every decimal digit in an int-field. It uses much more memory and is lower, but is more precice for operations like this.

I you KNOW the result will be an int-value, you can allways use new Double (myValue).intValue() . This will round the value to 0 fraction digits.


But do you really NEED the exact value?
If it's only for the outpur, you should write your own method to give for example only 3 digits (multiply the number per 1000, round it to int, convert it to String, insert the separator at the right place).

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Rounding error with double Posted: Nov 23, 2004 12:51 PM
Reply to this message Reply
Alternately, you might be able to work with longs and some 10-based mulitplier. For example, if you were working with time increments you might internally store milliseconds, but multiply by 1000 and print out results in seconds.

However, I would use BigDecimal first, as suggested by Mattias and switch to something like the above only if it is really necessary to improve performance or reduce memory usage.

Flat View: This topic has 2 replies on 1 page
Topic: Two Packages in 1 Directory Previous Topic   Next Topic Topic: Classpath problem with j2sdk142_nbide351

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use