programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# BigDecimal rounding problem

Ranch Hand
Posts: 32
referrinf to following code,

but my belief is if i use Rounding by BigDecimal.ROUND_HALF_EVEN then it should do round up for 4.225 to 4.23.
even the documentation suggests that way.. then y it doesnt do it??
any clues
Vijay

Vijay Dharap
Ranch Hand
Posts: 32
Sorry correct snippet is as following. sorry for all the trouble..

Ranch Hand
Posts: 116
Replace your round method with this, and you'll see why it does this:

[ February 04, 2005: Message edited by: Robert Hayes ]

Vijay Dharap
Ranch Hand
Posts: 32
thanks robert reason stood in front of my eyes,
But is there any other way to achieve what i want to achieve??

I want 4.225 to be rounded to 2 decimal places giving output as 4.23.

any help will be appriciated.

Sheriff
Posts: 11343
There are two things going on here.

First, as Robert's code illustrates, the BigDecimal values are probably not what you expect them to be. The reason is that they are initially doubles, and so they are stored according to IEEE 754 standards, which are not precise. Indeed, this is actually why we use BigDecimal. But in the code above, by the time these values are passed to the BigDecimal constructor, the precision is already gone.

The trick is in avoiding that floating-point representation. One way to do this is by using Strings instead of doubles. This is critical in using BigDecimal, and is explained in the API under the descriptions for BigDecimal constructors -- especially BigDecimal(double)...

http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html

So if you use the BigDecimal(String) constructor with Robert's addition to your code, you'll see that the BigDecimal values are exactly what you expect. But there's still a problem, and that brings us to the second issue...

I believe you want ROUND_HALF_UP instead of ROUND_HALF_EVEN.

[ February 04, 2005: Message edited by: marc weber ]

Vijay Dharap
Ranch Hand
Posts: 32
Hi Marc and Robert,

I tried your change by trying to pass the String to the BigDecimal's constructor. this is the code that i changed

Still output now shows exact 4.225 for bigdecimals value. but still rounding was not a success.
my output was

About the rounding way to use, i dont want to use ROUND_UP as the value for 4.224 must round to 4.22 while 4.225 and 4.226 should round to 4.23. Thats is what is most logical i believe.

Vijay Dharap
Ranch Hand
Posts: 32
hey marc,
it worked man.. with ROUND_HALF_UP, it worked.

thanks for pointing out both the problems in my code

thanks again.

marc weber
Sheriff
Posts: 11343
Yes, BigDecimal has a lot of different ways to round.

But as the API says about ROUND_HALF_UP, "Note that this is the rounding mode that most of us were taught in grade school."
[ February 04, 2005: Message edited by: marc weber ]

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?