# BigDecimal, setScale() and ROUND_HALF_UP...

Jim Hardin

Ranch Hand

Posts: 46

posted 8 years ago

- 0

Hello,

I am wondering why running the following:

as "java Test" returns

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

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

posted 8 years ago

- 0

I ended up doing the following:

which returns

-Jim

which returns

**0.00**, working the way I was expecting BigDecimal methodto work.setScale(2, BigDecimal.ROUND_HALF_UP)

-Jim

Jagan Nambi

Greenhorn

Posts: 15

posted 8 years ago

- 0

Hi Jim,

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

Regards,

Jagan.

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

Regards,

Jagan.

Jim Hardin

Ranch Hand

Posts: 46

Keith Lynn

Ranch Hand

Posts: 2399

Jim Hardin

Ranch Hand

Posts: 46

posted 8 years ago

- 0

Keith,

And scale of

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:

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

-Jim

[ April 04, 2007: Message edited by: Jim Harding ]

And scale of

**1**will doubtlessly round to**0.0**. I guess I wasn't clear that I'm doing financial calculations and, but I wanted to try to roundI'm trying to round to pennies.

**-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 ]

I agree. Here's the link: http://aspose.com/file-tools |