Problem with Double to BigDecimal Conversion

Discussion in 'Java' started by Forrest Hump, Aug 22, 2003.

  1. Forrest Hump

    Forrest Hump Guest

    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.1624999999999996447286321199499070644378662109375.
    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);
     
    Forrest Hump, Aug 22, 2003
    #1
    1. Advertising

  2. "Forrest Hump" <> 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.1624999999999996447286321199499070644378662109375.
    > 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
     
    Kent Paul Dolan, Aug 22, 2003
    #2
    1. Advertising

  3. Forrest Hump

    Dario Guest

    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, Aug 22, 2003
    #3
  4. Forrest Hump

    Dario Guest

    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, Aug 22, 2003
    #4
  5. Forrest Hump

    Mark Meyer Guest

    In our last episode, "Kent Paul Dolan" <> 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
    Raytheon Voice (972)344-0830 Fax (972)344-6840
     
    Mark Meyer, Aug 22, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. manzur
    Replies:
    14
    Views:
    7,622
    andyt
    Dec 16, 2010
  2. Sydex
    Replies:
    12
    Views:
    6,602
    Victor Bazarov
    Feb 17, 2005
  3. Replies:
    3
    Views:
    1,270
  4. Stanimir Stamenkov
    Replies:
    4
    Views:
    2,636
    Stanimir Stamenkov
    Jul 18, 2008
  5. Ramon
    Replies:
    12
    Views:
    1,365
Loading...

Share This Page