Rounding issue with BigDecimal and JSP
Hi
In my Java I have two BigDecimals that I pass to a JSP. THey both have a scale of 2, so I have
x = 5.52 and y =1.22
When I add them up in the JSP, the decimal places are lost e.g.
<c:out value="${ x + y)"/>
I get 6, instead of 6.74
Please can someone tell my why I get this rounding error?
Thanks!
# 1
How are you creating these BigDecimals?
# 2
Hi
Thanks for replying so quickly. The values I have are created by reading them from the database, where they are defined as Number(10,2) (this is an Oracle DB).
They get loaded up into the Java fine, with a value and the scale set to 2 (if you inspect them in a debugger).
It's only after they have been manipulated in the EL that the precision is lost. We're using JSP 1.2 in case that's of importance?
# 3
I had similar kind of issue sometime back. I solvedit using js function something like:var size = parseFloat(num1) + parseFloat(num2);
# 4
You're adding them as primitives. Try x.add(y).
# 5
Hi thanks to the last two posts.
I could do it in Javascript I guess, but I'd like to do it without having to declare any other variables e.g. in JavaScript. I'd rather just do the arithmetic in EL.
To HenrikStig, unfortunately I can't do that in EL. I could if I was still in the Java, but not in a JSP.
Thanks
# 6
OK, but have you considered doing <%= x.add(y) %> instead?
# 7
Thanks again. Yeah, I guess I can do it in scriptlets, but it's not as nice as doing it in JSTL. I don't have any scriptlets at the moment and I'd like to keep my JSPs scriptlets-free
In any case, I *should* surely be able to do this in JSTL/EL. So my main concern is whether EL is capable of doing this at all? Is it that bad at working with arithmetic?
# 8
What I meant was, specifically, what code are you using to construct the instances of BigDecimal?BTW, good plan, keeping your JSP scriptlet-free
# 9
Hi
It doesn't seem to matter how I construct the BigDecimals - I get the same behaviouor whether I read from a DB or create them from scratch. If I do it this way:
BigDecimal a = new BigDecimal(5.33).setScale(2, BigDecimal.ROUND_HALF_UP);
BigDecimal b = new BigDecimal(8.44).setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println("Result: " + a.add(b));
request.setAttribute("a", a);
request.setAttribute("b", b);
It prints out:
Result: 13.77(this is from the System.out btw)
If I print them out in the JSP, individually I get the correct result e.g.
<c:out value="${a}"/>-> Gives me 5.33
But if I then do
<c:out value="${a + b}"/>
It simply prints out '13', instead of 13.77
There must be something fundamentally wrong with what I'm doing but can;t figure it out!
# 10
For starters, you should use the constructor of BigDecimal that takes a string. That's what sets the precision, the apparent precision of the string. Try not setting the rounding mode, see how that affects your result
# 11
Thanks for the tips. There's nothing wrong with the BigDecimals as far as I can see though. Even using the string constructor and/or no rounding mode, I still get 13 instead of 13.77. Inspecting them with a debugger confirms that the BigDecimals are fine.
It seems to be simply that when they are passed to the JSP, they are evaluated incorrectly.
# 12
OK, I know what the problem is. In JSP 2 it's fine, but in my version of JSP a BigDecimal is coerced into a Long, losing it's precision. If you change it to a Double or a Float, it's fine. So its a limitation of versions below JSP2Thanks again for all the help