日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Thymeleaf】格式化数字

發布時間:2024/9/19 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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:
/** ======================================================================* See javadoc API for class org.thymeleaf.expression.Numbers* ======================================================================*//** ==========================* Formatting integer numbers* ==========================*//* * Set minimum integer digits.* Also works with arrays, lists or sets*/ ${#numbers.formatInteger(num,3)} ${#numbers.arrayFormatInteger(numArray,3)} ${#numbers.listFormatInteger(numList,3)} ${#numbers.setFormatInteger(numSet,3)}/* * Set minimum integer digits and thousands separator: * 'POINT', 'COMMA', 'WHITESPACE', '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')}/** ==========================* Formatting decimal numbers* ==========================*//** Set minimum integer digits and (exact) decimal digits.* Also works with arrays, lists or sets*/ ${#numbers.formatDecimal(num,3,2)} ${#numbers.arrayFormatDecimal(numArray,3,2)} ${#numbers.listFormatDecimal(numList,3,2)} ${#numbers.setFormatDecimal(numSet,3,2)}/** 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')}/** Set minimum integer digits and (exact) decimal digits, and also thousands and * decimal separator.* Also works with arrays, lists or sets*/ ${#numbers.formatDecimal(num,3,'POINT',2,'COMMA')} ${#numbers.arrayFormatDecimal(numArray,3,'POINT',2,'COMMA')} ${#numbers.listFormatDecimal(numList,3,'POINT',2,'COMMA')} ${#numbers.setFormatDecimal(numSet,3,'POINT',2,'COMMA')}/* * =====================* Formatting currencies* =====================*/${#numbers.formatCurrency(num)} ${#numbers.arrayFormatCurrency(numArray)} ${#numbers.listFormatCurrency(numList)} ${#numbers.setFormatCurrency(numSet)}/* * ======================* Formatting percentages* ======================*/${#numbers.formatPercent(num)} ${#numbers.arrayFormatPercent(numArray)} ${#numbers.listFormatPercent(numList)} ${#numbers.setFormatPercent(numSet)}/* * Set minimum integer digits and (exact) decimal digits.*/ ${#numbers.formatPercent(num, 3, 2)} ${#numbers.arrayFormatPercent(numArray, 3, 2)} ${#numbers.listFormatPercent(numList, 3, 2)} ${#numbers.setFormatPercent(numSet, 3, 2)}/** ===============* Utility methods* ===============*//** Create a sequence (array) of integer numbers going* from x to y*/ ${#numbers.sequence(from,to)} ${#numbers.sequence(from,to,step)}

參考

https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#numbers

總結

以上是生活随笔為你收集整理的【Thymeleaf】格式化数字的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。