Frage

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" />
War es hilfreich?

Lösung

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" />
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top