Home arrow static arrow Java Programming [Archive] - BigDecimal and Rounding
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - BigDecimal and Rounding
1 Duke Stars available
This topic has 3 replies on 1 page.

Posts:18
Registered: 10/16/01
BigDecimal and Rounding  
Jun 18, 2004 3:07 AM



 
I am trying to format double values into nice 2dp double values. Although when I do this I want to round up the values as well i.e.

10.0 -> 10.00
10.001 -> 10.01
10.008 -> 10.01
10.01 -> 10.01
10.0009 -> 10.01
10.02 -> 10.02

I am using this code to do that -


Double value = <some values, like those above>
BigDecimal b = new BigDecimal(value).setScale(2,BigDecimal.ROUND_UP);
System.out.println(b.doubleValue());


This works for all cases except when the value is 10.0 . The code above rounds it up to 10.01, which I dont want.

Does anyone know of a way that this can be done. I can't use DecimalFormat to format the double before I pass it into the BigDecimal constructor.

cheers,

Niraj

 

Posts:1,695
Registered: 1/13/04
Re: BigDecimal and Rounding  
Jun 18, 2004 5:29 AM (reply 1 of 3)



 
Not tested, but try this:
double dValue = 10.0d;  // or any valueint iValue = (int)dValue;int rounding = BigDecimal.ROUND_UP;double diff = dValue - (double)iValue;if (diff == 0) {   rounding = BigDecimal.ROUND_UNNECESSARY; }BigDecimal b = new BigDecimal(dValue).setScale(2, rounding);System.out.println("value = "+dValue+"  rounded = "+b.doubleValue());

Hope this will help,
regards.
 

Posts:1,183
Registered: 1/23/02
Re: BigDecimal and Rounding  
Jun 18, 2004 11:41 AM (reply 2 of 3)



 
I am trying to format double values into nice 2dp double values. Although when I do this I want to round up the values as well

Unfortunatelly you will have a lot of trouble with this. Many decimal numbers can't be represented exactly in floating-point binary. For example every power of 10 (excep zeroeth) will be represented as a number that's a little bigger than what you'd expect. You're seeing the effect of this imprecision blown up by the act of rounding.

I don't think Franck_Lefevre's solution will work because the imprecision is reintroduced when iValue is cast back to double.
 

Posts:1,695
Registered: 1/13/04
Re: BigDecimal and Rounding  
Jun 22, 2004 3:15 AM (reply 3 of 3)



 
I am trying to format double values into nice 2dp
double values. Although when I do this I want to round
up the values as well

Unfortunatelly you will have a lot of trouble with
this. Many decimal numbers can't be represented
exactly in floating-point binary. For example every
power of 10 (excep zeroeth) will be represented as a
number that's a little bigger than what you'd expect.
You're seeing the effect of this imprecision blown up
by the act of rounding.

I don't think Franck_Lefevre's solution will work
because the imprecision is reintroduced when iValue is
cast back to double.

You are right unless BigDecimal is used all along the calculating process, which wasn't explicitly
said by the original poster... In this case, i think the solution should work.
 
This topic has 3 replies on 1 page.