Bigdecimal division problem

Discussion in 'Java' started by manzur, Feb 23, 2006.

  1. manzur

    manzur Guest

    For the below Code snippet iam expecting 3333333.4 but iam getting
    4E+6.Could any one help me to understand the logic. thanks in advance

    BigDecimal quotient = BigDecimal.valueOf(10000000.0).divide(new
    BigDecimal(3),RoundingMode.CEILING);
     
    manzur, Feb 23, 2006
    #1
    1. Advertising

  2. manzur

    Bart Cremers Guest

    You'll need to set the scaling on your BigDecimal to the correct value
    before working on it. The way BigDecimal works is that it sets the
    scale to:

    "The scale of the returned BigDecimal is the smallest value such that
    (10scale × val) is an integer."

    In your case this is -6. Set the scale to 1 to get what you want:

    BigDecimal quotient =
    BigDecimal.valueOf(10000000.0).setScale(1).divide(new
    BigDecimal(3),RoundingMode.CEILING);

    Bart
     
    Bart Cremers, Feb 23, 2006
    #2
    1. Advertising

  3. manzur

    manzur Guest

    bart

    >From the ApI i found that

    If zero or positive, the scale is the number of digits to the right
    of the decimal point. If negative, the unscaled value of the number is
    multiplied by ten to the power of the negation of the scale. The value
    of the number represented by the BigDecimal is therefore (unscaledValue
    × 10-scale).

    So i think that there is no need to set the scale explicitly for
    positive numbers and the dividend in my code is also positive.
     
    manzur, Feb 23, 2006
    #3
  4. manzur

    manzur Guest

    bart
    but iam getting wht i want by following your process
     
    manzur, Feb 23, 2006
    #4
  5. manzur

    Bart Cremers Guest

    The sentence in quotes comes from the API as well. It's sometimes a
    problem to find the correct documentation in the API docs.

    But anyway, you get what you want... :)

    Bart
     
    Bart Cremers, Feb 23, 2006
    #5
  6. manzur wrote:
    > bart
    >
    >>From the ApI i found that

    > If zero or positive, the scale is the number of digits to the right
    > of the decimal point. If negative, the unscaled value of the number is
    > multiplied by ten to the power of the negation of the scale. The value
    > of the number represented by the BigDecimal is therefore (unscaledValue
    > × 10-scale).
    >
    > So i think that there is no need to set the scale explicitly for
    > positive numbers and the dividend in my code is also positive.
    >


    The "zero or positive" refers the scale, not the number.
    BigDecimal.valueOf(10000000.0) is equivalent to new BigDecimal("1.0E7")
    and so has scale -6, unscaled value 10.

    Using a double literal in BigDecimal valueOf is equivalent to conversion
    of the string representation of the literal to a double, conversion of
    the double to a String, and construction of a BigDecimal from that
    String. There is a risk of rounding errors, as well as unintended scaling.

    Unless there is some specific reason to involve doubles, I generally
    find it better to stick to direct construction from a String:

    new BigDecimal("10000000")

    Patricia
     
    Patricia Shanahan, Feb 23, 2006
    #6
    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. Forrest Hump
    Replies:
    4
    Views:
    24,287
    Mark Meyer
    Aug 22, 2003
  2. davo_java
    Replies:
    1
    Views:
    751
    davo_java
    Jun 23, 2005
  3. Replies:
    3
    Views:
    1,233
  4. Replies:
    94
    Views:
    4,498
    ¬a\\/b
    Feb 9, 2007
  5. Stanimir Stamenkov
    Replies:
    4
    Views:
    2,591
    Stanimir Stamenkov
    Jul 18, 2008
Loading...

Share This Page