BigDecimal 小数点后有 2 个小数?
我想要一个 BigDecimal 格式,小数点后保留 2 位小数。总是。
基于一些布尔检查,我有时会返回 BigDecimal.ZERO; 在本例中,JSF 页面仅显示“0”,尽管我设置了
。
我不明白这一点,但是我怎样才能强制使用 2 位数字呢?
I'd like to have a BigDecimal formatted with 2 decimals after the decimal point. Always.
Based on some boolean checks, I sometimes return a BigDecimal.ZERO;
In this case, the JSF page just displays "0", although I set <f:convertNumber minFractionDigits="2" maxFractionDigits="2"/>
.
I do not understand this, but nevertheless how can I enforce 2 digits else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有两种方法可以做到这一点,
您可以使用内置的 BigDecimal 函数。
这会返回一个小数点后两位的大小数,并且还会向上舍入。(还有其他舍入方法)
或者
您可以使用jsf ConvertNumber 标签
there are 2 ways of doing this
you can use the built in
BigDecimal
functionThis returns a big decmial with 2 digits past the decimal and also rounds up.(there are also other rounding methods)
OR
you could use jsf convertNumber Tag
尝试像这样格式化数字:
注意:另请注意有关接受答案中的四舍五入的警告。
Try formatting the number like this:
Note: Please also pay attention to the warnings regarding to rounding in the accepted answer.
试试这个
Try this out