posted 10 years ago
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
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
Jim Hardin
Ranch Hand
Posts: 46
Jim Hardin
Ranch Hand
Posts: 46
posted 10 years ago
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 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 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 ]
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 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 problembasically 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 roundedresult isalwaysnumericallygreaterthan the unroundedinput (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:I'm trying to round to pennies.
−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 ]
His brain is the size of a cherry pit! About the size of this ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
