【问题标题】:How to format the currency in HTML5 with thymeleaf如何使用 thymeleaf 格式化 HTML5 中的货币
【发布时间】:2012-12-19 01:52:38
【问题描述】:

我坚持在 HTML 5 中格式化货币。我有一个应用程序,我必须在其中格式化货币。我有以下代码 sn-p

 <td class="right"><span th:inline="text">$ [[${abc.value}]]</span></td>

我从 DAO abc 读取货币值的位置,它应该被格式化。 当前打印 $ 1200000.0 它应该打印 $ 1,200,000.0 .0

【问题讨论】:

    标签: java html thymeleaf currency-formatting


    【解决方案1】:

    您可以使用#numbers 实用程序对象,您可以在此处查看哪些方法:http://www.thymeleaf.org/apidocs/thymeleaf/2.0.15/org/thymeleaf/expression/Numbers.html

    例如:

    <span th:inline="text">$ [[${#numbers.formatDecimal(abc.value, 0, 'COMMA', 2, 'POINT')}]]</span>
    

    不过,您也可以在不进行内联的情况下执行此操作(这是 thymeleaf 推荐的方式):

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

    【讨论】:

    • 有没有可能删除零的10.00,嗯,只有在有小数的时候才显示小数会更干净,但是如果我们有一个小数,那么它会被四舍五入到@987654327 @??
    • 我也有同样的问题。见:stackoverflow.com/questions/52066661/…
    【解决方案2】:

    如果您的应用程序必须处理不同的语言,我建议使用 DEFAULT 值(= 基于区域设置):

    ${#numbers.formatDecimal(abc.value, 1, 'DEFAULT', 2, 'DEFAULT')}
    

    来自Thymeleaf doc(更准确地说是NumberPointType):

    /* 
     * Set minimum integer digits and thousands separator: 
     * 'POINT', 'COMMA', 'NONE' or 'DEFAULT' (by locale).
     * Also works with arrays, lists or sets
     */
    ${#numbers.formatInteger(num,3,'POINT')}
    ${#numbers.arrayFormatInteger(numArray,3,'POINT')}
    ${#numbers.listFormatInteger(numList,3,'POINT')}
    ${#numbers.setFormatInteger(numSet,3,'POINT')}
    
    /*
     * Set minimum integer digits and (exact) decimal digits, and also decimal separator.
     * Also works with arrays, lists or sets
     */
    ${#numbers.formatDecimal(num,3,2,'COMMA')}
    ${#numbers.arrayFormatDecimal(numArray,3,2,'COMMA')}
    ${#numbers.listFormatDecimal(numList,3,2,'COMMA')}
    ${#numbers.setFormatDecimal(numSet,3,2,'COMMA')}
    

    【讨论】:

    • 如果值低于1,例如0.55,那么这个表达式的结果就是.55。我建议将最小整数位数设置为1${#numbers.formatDecimal(abc.value, 1, 'DEFAULT', 2, 'DEFAULT')}。那么结果是0.55
    【解决方案3】:

    您现在可以更简单地在 numbers 实用程序中调用 formatCurrency 方法:

    #numbers.formatCurrency(abc.value)

    这也将不再需要货币符号。

    示例: &lt;span th:remove="tag" th:text="${#numbers.formatCurrency(abc.value)}"&gt;$100&lt;/span&gt;

    【讨论】:

    • Thymeleaf 的 API 中找不到此方法。
    • thymeleaf.org/apidocs/thymeleaf/3.0.9.RELEASE/org/thymeleaf/… 您可以点击Upvote撤消您的投票。
    • 如果我需要格式化不同的货币怎么办?欧元、英镑等
    • 当然,这超出了这个问题的范围,因为 OP 只关注显示美元(当前语言环境),并且不需要多种货币。但是在 GitHub 上有一个未解决的问题:github.com/thymeleaf/thymeleaf/issues/604。为了描述外部链接,将使用使用 formatDecimal 的较长形式,但可能会对 formatCurrency(number, locale) 方法感兴趣。
    【解决方案4】:

    您将使用 Thymeleaf 的 numbers 实用程序对象进行内联,如下所示:

    <span>[[${#numbers.formatCurrency(abc.value)}]]</span>
    

    在视图中,它甚至会为您添加美元符号 ($)。

    【讨论】:

    • 您将丢失带有内联的 span 标签之间的默认文本。直接在浏览器中打开页面(没有容器/服务器),您会看到不同之处。这是您将失去的 Thymeleaf 的主要优势。
    • 同意。原始帖子使用内联,我的建议是有偏见的。
    猜你喜欢
    • 1970-01-01
    • 2019-10-19
    • 2013-10-18
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 2011-06-29
    相关资源
    最近更新 更多