문제

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" />
도움이 되었습니까?

해결책

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" />
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top