【Thymeleaf】格式化数字
生活随笔
收集整理的這篇文章主要介紹了
【Thymeleaf】格式化数字
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
- thymeleaf 3.0
- spring boot 2.0.0RELEASE
左補0
<p th:with="num=10" th:utext="${#numbers.formatInteger(num,3)}">number format</p> <p th:with="num=10" th:utext="${#numbers.formatInteger(num,4)}">number format</p> <p th:with="num=10" th:utext="${#numbers.formatInteger(num,5)}">number format</p> <p th:with="num=10" th:utext="${#numbers.formatInteger(num,6)}">number format</p>輸出結果:
<p>010</p> <p>0010</p> <p>00010</p> <p>000010</p>千分位分隔符
<p th:with="num=1" th:utext="${#numbers.formatInteger(num,1,'COMMA')}">number format</p> <p th:with="num=10" th:utext="${#numbers.formatInteger(num,1,'COMMA')}">number format</p> <p th:with="num=100" th:utext="${#numbers.formatInteger(num,1,'COMMA')}">number format</p> <p th:with="num=1000" th:utext="${#numbers.formatInteger(num,1,'COMMA')}">number format</p> <p th:with="num=10000" th:utext="${#numbers.formatInteger(num,1,'COMMA')}">number format</p> <p th:with="num=100000" th:utext="${#numbers.formatInteger(num,1,'COMMA')}">number format</p> <p th:with="num=10" th:utext="${#numbers.formatInteger(num,2,'COMMA')}">number format</p> <p th:with="num=10" th:utext="${#numbers.formatInteger(num,3,'COMMA')}">number format</p> <p th:with="num=10" th:utext="${#numbers.formatInteger(num,4,'COMMA')}">number format</p> <p th:with="num=10" th:utext="${#numbers.formatInteger(num,5,'COMMA')}">number format</p> <p th:with="num=10" th:utext="${#numbers.formatInteger(num,6,'COMMA')}">number format</p>輸出結果:
<p>1</p> <p>10</p> <p>100</p> <p>1,000</p> <p>10,000</p> <p>100,000</p> <p>10</p> <p>010</p> <p>0,010</p> <p>00,010</p> <p>000,010</p>千分位分隔符 + 保留2位小數
<p th:with="num=1" th:utext="${#numbers.formatDecimal(num,3, 'COMMA', 2,'POINT')}">number format</p> <p th:with="num=10" th:utext="${#numbers.formatDecimal(num,3, 'COMMA', 2,'POINT')}">number format</p> <p th:with="num=100" th:utext="${#numbers.formatDecimal(num,3, 'COMMA', 2,'POINT')}">number format</p> <p th:with="num=1000" th:utext="${#numbers.formatDecimal(num,3, 'COMMA', 2,'POINT')}">number format</p> <p th:with="num=10000" th:utext="${#numbers.formatDecimal(num,3, 'COMMA', 2,'POINT')}">number format</p> <p th:with="num=100000" th:utext="${#numbers.formatDecimal(num,3, 'COMMA', 2,'POINT')}">number format</p> <p th:with="num=1000000" th:utext="${#numbers.formatDecimal(num,3, 'COMMA', 2,'POINT')}">number format</p> <p th:with="num=1234567.8" th:utext="${#numbers.formatDecimal(num,3, 'COMMA', 2,'POINT')}">number format</p> <p th:with="num=1234567.85" th:utext="${#numbers.formatDecimal(num,3, 'COMMA', 2,'POINT')}">number format</p> <p th:with="num=1234567.8797" th:utext="${#numbers.formatDecimal(num,3, 'COMMA', 2,'POINT')}">number format</p> <p th:with="num=1234567.8732" th:utext="${#numbers.formatDecimal(num,3, 'COMMA', 2,'POINT')}">number format</p>輸出結果:
<p>001.00</p> <p>010.00</p> <p>100.00</p> <p>1,000.00</p> <p>10,000.00</p> <p>100,000.00</p> <p>1,000,000.00</p> <p>1,234,567.80</p> <p>1,234,567.85</p> <p>1,234,567.88</p> <p>1,234,567.87</p>貨幣格式
轉換成貨幣格式時,是根據當前的 Locale 進行轉換。
<p th:with="num=1" th:utext="${#numbers.formatCurrency(num)}">number format</p> <p th:with="num=10" th:utext="${#numbers.formatCurrency(num)}">number format</p> <p th:with="num=100" th:utext="${#numbers.formatCurrency(num)}">number format</p> <p th:with="num=1000" th:utext="${#numbers.formatCurrency(num)}">number format</p> <p th:with="num=10000" th:utext="${#numbers.formatCurrency(num)}">number format</p> <p th:with="num=100000" th:utext="${#numbers.formatCurrency(num)}">number format</p> <p th:with="num=1000000" th:utext="${#numbers.formatCurrency(num)}">number format</p> <p th:with="num=1234567.8" th:utext="${#numbers.formatCurrency(num)}">number format</p> <p th:with="num=1234567.85" th:utext="${#numbers.formatCurrency(num)}">number format</p> <p th:with="num=1234567.8797" th:utext="${#numbers.formatCurrency(num)}">number format</p> <p th:with="num=1234567.8732" th:utext="${#numbers.formatCurrency(num)}">number format</p>Locale = zh_CN 時,輸出結果:
<p>¥1.00</p> <p>¥10.00</p> <p>¥100.00</p> <p>¥1,000.00</p> <p>¥10,000.00</p> <p>¥100,000.00</p> <p>¥1,000,000.00</p> <p>¥1,234,567.80</p> <p>¥1,234,567.85</p> <p>¥1,234,567.88</p> <p>¥1,234,567.87</p>Locale = en_US 時,輸出結果:
<p>$1.00</p> <p>$10.00</p> <p>$100.00</p> <p>$1,000.00</p> <p>$10,000.00</p> <p>$100,000.00</p> <p>$1,000,000.00</p> <p>$1,234,567.80</p> <p>$1,234,567.85</p> <p>$1,234,567.88</p> <p>$1,234,567.87</p>Locale = de_DE 時,輸出結果:
<p>1,00 €</p> <p>10,00 €</p> <p>100,00 €</p> <p>1.000,00 €</p> <p>10.000,00 €</p> <p>100.000,00 €</p> <p>1.000.000,00 €</p> <p>1.234.567,80 €</p> <p>1.234.567,85 €</p> <p>1.234.567,88 €</p> <p>1.234.567,87 €</p>百分比格式
<p th:with="num=1" th:utext="${#numbers.formatPercent(num, 1, 2)}">number format</p> <p th:with="num=1" th:utext="${#numbers.formatPercent(num, 2, 2)}">number format</p> <p th:with="num=1" th:utext="${#numbers.formatPercent(num, 3, 2)}">number format</p> <p th:with="num=1" th:utext="${#numbers.formatPercent(num, 4, 2)}">number format</p> <p th:with="num=0.80" th:utext="${#numbers.formatPercent(num, 1, 2)}">number format</p> <p th:with="num=0.808" th:utext="${#numbers.formatPercent(num, 1, 2)}">number format</p> <p th:with="num=0.80881" th:utext="${#numbers.formatPercent(num, 1, 2)}">number format</p> <p th:with="num=0.80888" th:utext="${#numbers.formatPercent(num, 1, 2)}">number format</p> <p th:with="num=3.1415926" th:utext="${#numbers.formatPercent(num, 1, 3)}">number format</p>輸出結果:
<p>100.00%</p> <p>100.00%</p> <p>100.00%</p> <p>0,100.00%</p> <p>80.00%</p> <p>80.80%</p> <p>80.88%</p> <p>80.89%</p> <p>314.159%</p>Thymeleaf 中的 numbers 對象
- #numbers : utility methods for number objects:
參考
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#numbers
總結
以上是生活随笔為你收集整理的【Thymeleaf】格式化数字的全部內容,希望文章能夠幫你解決所遇到的問題。