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 ]