问题
I need to format number using <fmt:formatNumber/>
jstl tag. The output should be restricted to 3 places after the decimal point, but rounding isn't allowed, so using the attribute maxFractionDigits="3"
isn't appropriate, cause it rounds the number.
Do you have any suggestions?
回答1:
You could subtract 0.0005 from the number before formatting it. That way the rounding will be equivalent to truncating the original number to 3 decimal places.
<fmt:formatNumber value="${myNumber - 0.0005}" maxFractionDigits="3"/>
回答2:
Design your own tag or EL function that truncates the number before formatting it. Or just design a tag or function that truncates the number, and pass the result to <fmt:formatNumber/>
. Something like
<fmt:formatNumber value="${myFn:truncate(theNumber, 3)"/>
来源:https://stackoverflow.com/questions/13175225/format-number-with-jstl-and-no-rounding