Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

BigDecimal Round Half Up - Not Working Properly

807589Oct 22 2008 — edited Oct 22 2008
BigDecimal's ROUND_HALF_UP method is producing some strange results for me. I'm trying to round off a double to decimal places.

Here is the description of the method from the API

"Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. Behaves as for ROUND_UP if the discarded fraction is >= .5; otherwise, behaves as for ROUND_DOWN. Note that this is the rounding mode that most of us were taught in grade school."

However, I've found that if the discarded fraction =5 it rounds down, not up.

e.g.
bd = new BigDecimal(12.825);
bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
double result = bd.doubleValue();
result=12.82!!!!

I noticed on IBM's list of JAVA fixes, it mentions a fix to BigDecimal's Round_Half_Up method but for older versions of Java (1.4). I'm running the newest version, but surely this is the same problem they're talking about.

Please help!!!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 19 2008
Added on Oct 22 2008
6 comments
3,791 views