【问题标题】:Convert cent value in integer to currency in HTML with thymleaf使用 thymeleaf 将整数中的美分值转换为 HTML 中的货币
【发布时间】:2020-01-10 03:48:15
【问题描述】:

如何将整数中的美分值显示为thymleaf中的货币?

我正在集成一个银行 API,该 API 以美分返回所有金额。我使用 thymleaf 作为模板引擎,无法在前端正确显示金额。

例如:

来自服务器的值 = 16417
预期输出 = 164.17

我尝试了以下表达式,但无法获得预期的输出。

1)

<span th:text="${#numbers.formatDecimal(page.amount, 0, 'COMMA', 2, 'POINT')}"></span>

输出:16,417.00

2)

<span th:text="${#numbers.formatDecimal(page.amount/100, 0, 'COMMA', 2, 'POINT')}"></span>

输出:164.00

3)

<span th:text="${#numbers.formatCurrency(page.amount)}"></span>

输出:$16,417.00

4)

<span th:text="${#numbers.formatCurrency(page.amount/100)}"></span>

输出:$164.00

【问题讨论】:

    标签: java html spring-boot thymeleaf


    【解决方案1】:

    您已经很接近了,但是,Java 正在执行整数除法并且只是删除小数/小数部分。尝试强制程序执行“常规”除法:

    <span th:text="${#numbers.formatCurrency(page.amount/100.00)}"></span>
    

    <span th:text="${#numbers.formatDecimal(page.amount/100.00, 0, 'COMMA', 2, 'POINT')}"></span>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-15
      • 2022-12-03
      • 2022-01-11
      • 2016-11-22
      • 1970-01-01
      • 2015-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多