Question

I have a number that I want to be truncated not rounded. It seems like when I use minFractionalDigit=2 my number gets rounded on the third decimal.

For example:

What happens:

1.123 => 1.12
1.145 => 1.15
1 => 1.00

What I want: 

1.123 => 1.12
1.145 => 1.14
1 => 1.00    


<fmt:formatNumber value="${myNumber}" minFractionDigits="2" type="currency" />
Was it helpful?

Solution

Here is a front-end approach that circumvents rounding.

Another approach would be to format the values on the back-end like so:

BigDecimal formattedNumber = new BigDecimal(myNumber);
format.setScale(2, RoundingMode.DOWN);

request.setAttribute("myNumber", formattedNumber);

Which you can then reference in an expression:

<fmt:formatNumber value="${myNumber}" minFractionDigits="2" type="currency" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top