我坚持格式化货币,在HTML 5,我有应用程序,我不得不格式使用的货币。我有下面的代码片段
<td class="right"><span th:inline="text">$ [[${abc.value}]]</span></td>
凡从DAO ABC我读了货币的价值,它应该被格式化。目前,印刷$ 1200000.0它应该打印$ 1,200,000.0 0.0
您可以使用#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>
我建议在你的应用程序必须处理不同语言的情况下使用(基于区域=)的默认值:
${#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')}
现在,您可以更简单地调用在formatCurrency
实用的numbers
方法:
#numbers.formatCurrency(abc.value)
这将删除一个货币符号的需求也是如此。
例如:<span th:remove="tag" th:text="${#numbers.formatCurrency(abc.value)}">$100</span>
你会使用内嵌Thymeleaf的数字工具对象,如下所示:
<span>[[${#numbers.formatCurrency(abc.value)}]]</span>
在视图中,它甚至会在前面加上你的美元符号($)。