Format number with JSTL and no rounding

半城伤御伤魂 提交于 2020-01-24 01:28:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!