The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Re: Unexpected double value

Posted by Brian Sanders on November 28, 2001 at 1:44 PM

Sun's Java Developer Connection had a goog Tech Tip on this issue. The essence of the problem is this: a double is stored in binary floating-point representation. There is no exact floating-point representation for some numbers (like 0.1), so the real double value is the closest floating-point value to 0.1. In binary, this value is 1001100110011001100110011001100110011001100110011010.

The class java.math.BigDecimal uses a non-floating-point representation for numbers. So, this value:

java.math.BigDecimal oneTenth = new java.math.BigDecimal("0.1");

is exactly equal to 0.1 Use BigDecimal in your program, and you'll get the expected behavior.


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us