Thymeleaf mainly uses the org.thymeleaf.expression.Numbers class to process numbers, and the #numbers object to process numbers in templates.
Development environment: IntelliJ IDEA 2019.2.2
Spring Boot version: 2.1.8
Create a new Spring Book project named demo.
pom.xml adds Thymeleaf dependencies:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
I. Integer Formatting
There are four methods:
(1)formatInteger(number,digits)
The first parameter is a single number, if there is a small number point, it is rounded, and the second parameter sets the least integer number, which will make up for 0 (the same below).
(2)arrayFormatInteger(numbers,digits)
Pass in the array and return to the processed array
(3)listFormatInteger(numbers,digits)
Pass in the List and return to the processed List
(4)setFormatInteger(numbers,digits)
Pass in Set and return to the processed Set
These four methods have overloaded methods that pass in a third parameter to identify a thousand-bit separator
POINT: use "."
COMMA: Use ",".
WHITESPACE: Use "(space)
NONE: No delimiters are used
DEFAULT: Depending on the Locale object
1,src/main/resources/templates/integer.html
formatInteger(number,digits) <div th:text="${#numbers.formatInteger(10,0)}"></div> <div th:text="${#numbers.formatInteger(10.6,2)}"></div> <div th:text="${#numbers.formatInteger(10.6,5)}"></div> <div th:text="${#numbers.formatInteger(10.50,0)}"></div> <div th:text="${#numbers.formatInteger(10.51,2)}"></div> <div th:text="${#numbers.formatInteger(10000000,0,'COMMA')}"></div> <div th:text="${#numbers.formatInteger(10000000,0,'POINT')}"></div> arrayFormatInteger(numbers,digits) <div th:each="num : ${#numbers.arrayFormatInteger(arr,0)}"> <div th:text="${num}"></div> </div> listFormatInteger(numbers,digits) <div th:each="num : ${#numbers.listFormatInteger(list,2)}"> <div th:text="${num}"></div> </div> setFormatInteger(numbers,digits) <div th:each="num : ${#numbers.setFormatInteger(set,4)}"> <div th:text="${num}"></div> </div>
2,src/main/java/com/example/demo/IntegerController.java
package com.example.demo; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; @Controller public class IntegerController { @RequestMapping("/integer") public String integer(Model model){ Double[] arr = new Double[]{10D, 10.9}; List list = Arrays.asList(arr); Set set = new HashSet(list); model.addAttribute("arr", arr); model.addAttribute("list", list); model.addAttribute("set", set); return "integer"; } }
Browser access: http://localhost:8080/integer
Page output:
formatInteger(number,digits) 10 11 00011 10 11 10,000,000 10.000.000 arrayFormatInteger(numbers,digits) 10 11 listFormatInteger(numbers,digits) 10 11 setFormatInteger(numbers,digits) 0010 0011
2. Decimal Formatting
There are also four ways:
(1)formatDecimal(number,intDig,decDig)
The first parameter is a single number, the second parameter sets the smallest integer number (less than 0 will be added), and the third parameter sets the reserved decimal number.
(2)arrayFormatDecimal(numArray,intDig,decDig)
Pass in the array and return to the processed array
(3)listFormatDecimal(numList,intDig,decDig)
Pass in the List and return to the processed List
(4)setFormatDecimal(numSet,intDig,decDig)
Pass in Set and return to the processed Set
There are two overloading methods for each of these four methods. Take formatDecimal as an example:
(a)formatDecimal(number,intDig,decDig,decPoint)
decPoint denotes what symbol is used as the decimal point and takes the values POINT, COMMA, WHITESPACE, NONE and DEFAULT.
(b)formatDecimal(number,intDig,separator,decDig,decPoint)
Separator denotes what symbol is used as a 1000-bit separator, with the same values as POINT, COMMA, WHITESPACE, NONE and DEFAULT.
1,src/main/resources/templates/decimal.html
<div th:text="${#numbers.formatDecimal(10, 0, 0)}"></div> <div th:text="${#numbers.formatDecimal(10.6, 0, 2)}"></div> <div th:text="${#numbers.formatDecimal(10.6, 5, 2)}"></div> <div th:text="${#numbers.formatDecimal(10000000, 0, 2, 'COMMA')}"></div> <div th:text="${#numbers.formatDecimal(10000000, 2, 2, 'POINT')}"></div> <div th:text="${#numbers.formatDecimal(10000000, 2, 'POINT', 2, 'POINT')}"></div>
2,src/main/java/com/example/demo/DecimalController.java
package com.example.demo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class DecimalController { @RequestMapping("/decimal") public String decimal(){ return "decimal"; } }
Browser access: http://localhost:8080/decimal
Page output:
10 10.60 00010.60 10000000,00 10000000.00 10.000.000.00
3. Percentage Formatting
Similar to decimal formatting, there are four ways to process a single number
formatPercent(number,intDig,decDig)
The first parameter is a single number, the second parameter sets the smallest integer number (less than 0 will be added), and the third parameter sets the reserved decimal number.
1,src/main/resources/templates/percent.html
<div th:text="${#numbers.formatPercent(0.123, 0, 2)}"></div> <div th:text="${#numbers.formatPercent(0.123, 5, 2)}"></div>
2,src/main/java/com/example/demo/PercentController.java
package com.example.demo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class PercentController { @RequestMapping("/percent") public String percent(){ return "percent"; } }
Browser access: http://localhost:8080/percent
Page output:
12.30% 00,012.30%
sequence METHOD
The sequence method returns the Integer array.
(1)sequence(from,to)
Set start and end values. If from is larger than to, the default step size is 1, otherwise - 1.
(2)sequence(from,to,step)
Set start and end values, step size.
1,src/main/resources/templates/sequence.html
<div th:each="num : ${#numbers.sequence(0,3)}"> <div th:text="${num}"></div> </div> ---------- <div th:each="num : ${#numbers.sequence(5,1)}"> <div th:text="${num}"></div> </div>
2,src/main/java/com/example/demo/SequenceController.java
package com.example.demo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class SequenceController { @RequestMapping("/sequence") public String sequence(){ return "sequence"; } }
Browser access: http://localhost:8080/percent
Page output:
0 1 2 3 ---------- 5 4 3 2 1