Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# BigDecimal, setScale() and ROUND_HALF_UP...

Jim Hardin
Ranch Hand
Posts: 46
Hello,
I am wondering why running the following:

as "java Test" returns -0.01 and not 0.00?
In my particular case, "num" is the result of some lengthy algebra, which in the end, I'm trying to round to pennies. I believe I have read the JavaDoc on java.math.BigDecimal for setScale() and ROUND_HALF_UP and am not closer to understanding this. The table in RoundingMode JavaDoc suggests that this is just the way it works. What I would like to accomplish is to round half up, regardless of sign, similar to the way the Number.toFixed(2) works in JavaScript. Thank you for reading and any and all assistance.
-Jim

Jim Hardin
Ranch Hand
Posts: 46
I ended up doing the following:

which returns0.00, working the way I was expecting BigDecimal method
setScale(2, BigDecimal.ROUND_HALF_UP)
to work.
-Jim

Jagan Nambi
Greenhorn
Posts: 15
Hi Jim,

There's no such method called toPlainString() within the java.math.BigDecimal APIs.

Regards,
Jagan.

Jim Hardin
Ranch Hand
Posts: 46
It is a
Since: 1.5

See:
http://java.sun.com/j2se/1.5.0/docs/api/
-Jim

Keith Lynn
Ranch Hand
Posts: 2409
Notice that the scale is the number of digits after the decimal point.

If you change your original code to

that will round your value to 0.

Jim Hardin
Ranch Hand
Posts: 46
Keith,
And scale of 1 will doubtlessly round to 0.0. I guess I wasn't clear that I'm doing financial calculations and
I'm trying to round to pennies.
, but I wanted to try to round -0.005 (minus 1/2 of a cent) up, not to zero dollars (\$0), but up to zero dollars and zero cents (\$0.00), always tracking pennies. With ROUND_HALF_UP, I was instead getting minus one cent (which I would intuitively call rounding down), which was my problem--basically getting my brain around what ROUND_HALF_UP is doing -- ROUND_HALF_UP is increasing the magnitude of the number regardless of sign (whoa?), and I was expecting it to increase the number itself such that the rounded-result is-always-numerically-greater-than the unrounded-input (up?), which is the way the JavaScript Number method toFixed(2) works. To Java's defense (and my chagrin), it sounds like wikipedia agrees with Java's ROUND_HALF_UP techninque (see http://en.wikipedia.org/wiki/Rounding_numbers) for:
−2.1349 rounded to hundredths is −2.13
−2.1350 rounded to hundredths is −2.14

I would have also rounded the latter to -2.13, since -2.13 is numerically greater than -2.1350. Anyway, I was trying to remember (see ROUND_HALF_UP JavaDoc) for the following excerpts:
Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. Note that this is the rounding mode that most of us were taught in grade school.

back to when I learned to round negative numbers, and I'm pretty sure when I was in grade school, I learned to round negative numbers the way the above scaleAndRound() static method works and not the way ROUND_HALF_UP works. I must admit that I feel I was misled by the documentation, though I am impressed by the variety of BigDecimal rounding methods available, and surprised that there is nothing out of the box which matches what I was looking for, which I feel would be a very common rounding usage. I think my problem is solved. Thank you very much.
-Jim
[ April 04, 2007: Message edited by: Jim Harding ]