2013-01-04 59 views
18

我被格式化爲HTML 5中的貨幣。我有應用程序必須格式化貨幣。我有下面的代碼片段如何使用百里香葉格式化HTML5中的貨幣

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

從DAO abc我讀的貨幣值,它應該格式化。 目前打印$ 1200000.0它應該打印$ 1,200,000.0 0.0

回答

34

可以使用#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> 
+4

是否有可能去除零的'10.00',嗯這將是更乾淨,只顯示小數時有什麼,但如果我們有一個十進制數,所以它得到圓到'435.89'? – azerafati

7

I reco mmend的情況下使用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')} 
0

您現在可以更簡單地撥打numbers工具中的formatCurrency方法:

#numbers.formatCurrency(abc.value)

這也將免除對貨幣符號的需求。

例子: <span th:remove="tag" th:text="${#numbers.formatCurrency(abc.value)}">$100</span>