Velocity Reviews > Java > Problem with Double to BigDecimal Conversion

Problem with Double to BigDecimal Conversion

Forrest Hump
Guest
Posts: n/a

 08-22-2003
Can anyone tell me why the code sample below returns a different value for
variable b_OLD ?

The value I get for bd1 is 4.162499999999999644728632119949907064437866210937 5.
Why does Java not keep it as 4.1625 ?

----------------

double b_OLD = 4.1625;

java.math.BigDecimal bd1 = new java.math.BigDecimal( b_OLD );

System.out.println("BEFORE ROUNDING: " + bd1);

bd1 = bd1.setScale(0, java.math.BigDecimal.ROUND_CEILING);
System.out.println("AFTER ROUNDING: " + bd1);

Kent Paul Dolan
Guest
Posts: n/a

 08-22-2003
"Forrest Hump" <(E-Mail Removed)> wrote:

> Can anyone tell me why the code sample below returns a different value for
> variable b_OLD ?

> The value I get for bd1 is 4.162499999999999644728632119949907064437866210937 5.
> Why does Java not keep it as 4.1625 ?

> double b_OLD = 4.1625;
>
> java.math.BigDecimal bd1 = new java.math.BigDecimal( b_OLD );

The usual reason: the number whose decimal representation is "4.1625"
cannot be exactly represented as a "binary number with binary point" in
any finite number of bits (and you only get 52 or so), and so the first
roughly 17 decimal digits of the BigDecimal representation are the best
that the double representation could do in binary when expressed instead
in decimal; what the rest is, I don't know, maybe trash, maybe the
remains of the roundoff back from binary to decimal.

xanthian.

--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG

Dario
Guest
Posts: n/a

 08-22-2003
Forrest Hump wrote:

> double b_OLD = 4.1625;
> java.math.BigDecimal bd1 = new java.math.BigDecimal( b_OLD );
> System.out.println("BEFORE ROUNDING: " + bd1);

Try:

java.math.BigDecimal bd1 = new java.math.BigDecimal("4.1625");

I hope this will solve your problem.

- Dario

Dario
Guest
Posts: n/a

 08-22-2003
Forrest Hump wrote:

> double b_OLD = 4.1625;
> java.math.BigDecimal bd1 = new java.math.BigDecimal( b_OLD );
> System.out.println("BEFORE ROUNDING: " + bd1);

Try:

java.math.BigDecimal bd1 = new java.math.BigDecimal("4.1625");

I hope this will solve your problem.

- Dario

Mark Meyer
Guest
Posts: n/a

 08-22-2003
In our last episode, "Kent Paul Dolan" <(E-Mail Removed)> wrote:
>The usual reason: the number whose decimal representation is "4.1625"
>cannot be exactly represented as a "binary number with binary point" in
>any finite number of bits (and you only get 52 or so), and so the first
>roughly 17 decimal digits of the BigDecimal representation are the best
>that the double representation could do in binary when expressed instead
>in decimal; what the rest is, I don't know, ...

The BigDecimal number representation is the exact decimal
representation of the double's binary approximation of 4.1625.

--
Mark Meyer

Mark Meyer http://www.velocityreviews.com/forums/(E-Mail Removed)
Raytheon Voice (972)344-0830 Fax (972)344-6840

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post manzur Java 14 12-16-2010 03:35 PM Ramon Java 12 11-03-2008 12:58 PM Stanimir Stamenkov Java 4 07-18-2008 10:49 AM czarnysfetr@gmail.com Java 3 02-05-2007 02:49 PM Sydex C++ 12 02-17-2005 06:30 PM