如何使用thymeleaf格式化HTML5中的货币
我在格式化HTML 5中的货币时遇到困难。我有应用程序,我必须格式化货币。 我有以下代码片段
$ [[${abc.value}]]
从DAO abc我在读取货币值时,应格式化。 目前打印$ 1200000.0应打印$ 1,200,000.0 .0
您可以使用#numbers
实用程序对象,您可以在此处看到哪些方法: http : //www.thymeleaf.org/apidocs/thymeleaf/2.0.15/org/thymeleaf/expression/Numbers.html
例如:
$ [[${#numbers.formatDecimal(abc.value, 0, 'COMMA', 2, 'POINT')}]]
不过,您也可以在没有内联的情况下执行此操作(这是百事可乐建议的方式):
$ 10.00
如果您的应用程序必须处理不同的语言,我建议使用DEFAULT值(=基于语言环境):
${#numbers.formatDecimal(abc.value, 0, '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')}
您现在可以更简单地在numbers
实用程序中调用formatCurrency
方法:
#numbers.formatCurrency(abc.value)
这也将消除对货币符号的需要。
示例: $100