BigDecimal 小数点后有 2 个小数?

发布于 2025-01-08 04:51:56 字数 239 浏览 2 评论 0原文

我想要一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

中性美 2025-01-15 04:51:56

有两种方法可以做到这一点,

您可以使用内置的 BigDecimal 函数。

myBigDecimal.SetScale(2, RoundingMode.HALF_UP);

这会返回一个小数点后两位的大小数,并且还会向上舍入。(还有其他舍入方法)

或者

您可以使用jsf ConvertNumber 标签

<f:convertNumber maxFractionDigits="2" minFractionDigits="2" />

there are 2 ways of doing this

you can use the built in BigDecimal function

myBigDecimal.SetScale(2, RoundingMode.HALF_UP);

This 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

<f:convertNumber maxFractionDigits="2" minFractionDigits="2" />
口干舌燥 2025-01-15 04:51:56

尝试像这样格式化数字:

    <h:outputText value="#{myBean.bd1}">
        <f:convertNumber pattern="0.00" />
    </h:outputText>

注意:另请注意有关接受答案中的四舍五入的警告。

Try formatting the number like this:

    <h:outputText value="#{myBean.bd1}">
        <f:convertNumber pattern="0.00" />
    </h:outputText>

Note: Please also pay attention to the warnings regarding to rounding in the accepted answer.

月隐月明月朦胧 2025-01-15 04:51:56

试试这个

<af:convertNumber maxIntegerDigits="3" maxFractionDigits="2"   minFractionDigits="2" pattern="###.##" />

Try this out

<af:convertNumber maxIntegerDigits="3" maxFractionDigits="2"   minFractionDigits="2" pattern="###.##" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文