前言

  • 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】格式化数字相关推荐

  1. 在JavaScript中使用正好两位小数格式化数字

    我有这行代码将我的数字四舍五入到小数点后两位. 但是我得到这样的数字:10.8.2.4等.这些不是我对小数点后两位的想法,因此我如何改善以下内容? Math.round(price*Math.pow( ...

  2. oracle空格转换函数,ORACLE TO_CHAR函数格式化数字的出现空格的缘故

    ORACLE TO_CHAR函数格式化数字的出现空格的原因 在这篇博客SQL挑战--如何高效生成编码里面我由于需要将数字格式化为字符,像12需要格式化0012这样的字符,所以使用了TO_CHAR(数字 ...

  3. 在jsp页面中实现格式化数字,百分比,货币

    当时的要求是在jsp页面中计算百分比 实现方法 1.引入Jstl的fmt指令 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" ...

  4. php三位不够前面加0,php 格式化数字 位数不足前面加0补足的实现方法

    php 格式化数字,位数不足时前面加0补足,本文提供了两种实现方法,分别用sprintf与number_format来实现.有需要的朋友,快来看看吧. php格式化数字的例子. 一.字符串sprint ...

  5. Oracle处理小数点后位数、格式化数字、查找指定字符所在位置的几个函数

    转载自:http://www.cnblogs.com/toowang/p/3781480.html 1.处理小数点位数的几个oracle函数(): 1.取四舍五入的几位小数 select round( ...

  6. JavaScript格式化数字显示格式

    为什么80%的码农都做不了架构师?>>>    JavaScript格式化数字显示格式 /** * 格式化数字显示方式 * 用法 * formatNumber(12345.999,' ...

  7. php格式化数字:位数不足前面加0补足

    原文地址为: php格式化数字:位数不足前面加0补足 php格式化数字:位数不足前面加0补足 先实例,后讲解 PHP代码 <?php         $var= sprintf("%0 ...

  8. JAVA中计算百分比 格式化数字

    JAVA中计算百分比 格式化数字 这个是我在程序使用的例子: public String myPercent(int y,int z){    String baifenbi="" ...

  9. php 数字货币格式化,NPM酷库:accounting,格式化数字和货币

    NPM酷库,每天两分钟,了解一个流行NPM库.· 上次,我们了解到如何使用numeral库格式化数字.今天我们继续认识另外一个用来格式化数字的库accounting. accounting accou ...

  10. java输出数字格式化_Java™ 教程(格式化数字打印输出)

    格式化数字打印输出 以前你已经看到使用print和println方法将字符串打印到标准输出(System.out),因为全部数字均可以转换为字符串(你将在本课后面看到),你可使用这些方法打印出任意的字 ...

最新文章

  1. [UWP小白日记-14]正则表达式
  2. 关于1970-1-1 00:00.000的知识【转】
  3. linux数据被删了怎么办
  4. 吴恩达老师的机器学习和深度学习课程笔记打印版
  5. python 文件遍历
  6. Kotlin入门(28)Application单例化
  7. IT职场人生系列之十七:入职(高手篇)
  8. as3程序主类,执行顺序
  9. tcp 状态转移图详解
  10. SpringMVC-视图和视图解析器
  11. mem考试能用计算机吗,Memtest可以通过多少次?
  12. 【Pyqt5】实现小学三年级口算题生成器
  13. 每日思考第 61 期:职场PUA与情场PUA
  14. 计算机各类会议及投稿文章总结,个人感觉入门超级有用!
  15. 不定高度的slideUp动画效果
  16. Nginx (一) Nginx介绍 正向代理 反向代理 及配置
  17. oracle通过imp导出数据库时提示:这些对象由***导出,而不是当前用户解决方法
  18. oracle sql数据计算精度问题
  19. Java方法excel文件转换成xml文件
  20. 经济学“边际效应”VS根正苗红的RPA

热门文章

  1. VMware vSphere Replication 5.5 安装配置【展现虚拟化商业价值征文大赛】
  2. Android实现自定义的 时间日期 控件
  3. 狄克斯特拉算法(入门)
  4. 如何修改IE浏览器的User-Agent用户代理字符串信息
  5. C#中DictionaryTKey,TValue排序方式
  6. 团购、定时抢购倒计时js版
  7. 快钱支付与Sql Server的乐观锁和悲观锁
  8. ASP.NET GetPostBackEventReference
  9. java 三种将list转换为map的方法详解
  10. Thinkphp5中异常处理不返回页面返回Json格式的字符串