aspose file tools*
The moose likes Java in General and the fly likes BigDecimal rounding problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "BigDecimal rounding problem" Watch "BigDecimal rounding problem" New topic
Author

BigDecimal rounding problem

Vijay Dharap
Ranch Hand

Joined: Mar 18, 2004
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,
<a href="http://dharapvj.wordpress.com" rel="nofollow">Blog</a>
Vijay Dharap
Ranch Hand

Joined: Mar 18, 2004
Posts: 32
Sorry correct snippet is as following. sorry for all the trouble..
Robert Hayes
Ranch Hand

Joined: Oct 24, 2004
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

Joined: Mar 18, 2004
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.
marc weber
Sheriff

Joined: Aug 31, 2004
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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Vijay Dharap
Ranch Hand

Joined: Mar 18, 2004
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

Joined: Mar 18, 2004
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

Joined: Aug 31, 2004
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: BigDecimal rounding problem