【问题标题】:Thymeleaf - formatting percentage with only two decimal numbersThymeleaf - 只有两个十进制数字的格式百分比
【发布时间】:2017-03-02 04:04:40
【问题描述】:

我正在尝试使用 Thymeleaf 格式化百分比。 但是,对于 99.99 这样的 %,thymeleaf 将这个值格式化为 100。我不想要它。

我这样做了:

Java 端

BigDecimal percentage = (a).multiply(new BigDecimal(100)).divide(b, 3, RoundingMode.HALF_DOWN);

百里香一面

th:text="${#numbers.formatDecimal(percentage, 1, 'POINT', 2, 'COMMA')}"

如果 百分比 是 99.99 Thymeleaf 给我 100.00

为什么?

【问题讨论】:

    标签: java formatting thymeleaf percentage bigdecimal


    【解决方案1】:

    发生这种情况是因为您在 Java 端的 percentagescale 值大于在 Thymeleaf 端的 decimalDigits

    尝试将其设置为相同的值(为 2):

    BigDecimal percentage = (a).multiply(new BigDecimal(100)).divide(b, 2, RoundingMode.HALF_DOWN);
    

    而且好像您仅使用 percentage 字段来显示百分比一样,我认为您不需要 thousandsPointType 参数(在您的情况下为 'POINT'),因为它的值永远不会大于 100。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 2012-11-17
      • 1970-01-01
      相关资源
      最近更新 更多