Use of Thymeleaf Objects: Digital Objects

Thymeleaf mainly uses the org.thymeleaf.expression.Numbers class to handle numbers, and uses the #numbers object in the template to handle numbers.

Development environment: IntelliJ IDEA 2019.2.2
Spring Boot version: 2.1.8

Create a new Spring Boot project named demo.
Add Thymeleaf dependency to pom.xml:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

One, integer formatting

There are 4 methods:
(1) formatInteger(number, digits) The 
first parameter is a single number, if there is a decimal point, it will be rounded up, and the second parameter is set to the minimum number of integer digits. If it is not enough, it will be filled with 0 (the same below)
( 2) ArrayFormatInteger(numbers, digits) is 
passed to the array, and the processed array is returned
(3) listFormatInteger(numbers, digits) is 
passed to the List, and the processed List is returned
(4) setFormatInteger(numbers, digits) is 
passed to the Set, and the processed array is returned. The last
four methods of Set have overloaded methods to pass in the third parameter, which is used to identify the thousands separator.
POINT: use "."
COMMA: use ","
WHITESPACE: use "" (space)
NONE: do not use separation Character
DEFAULT: Determined according to 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)
11
10
10,000,000
10.000.000
arrayFormatInteger(numbers,digits)
11
listFormatInteger(numbers,digits)
11
setFormatInteger(numbers,digits)
0011

Second, decimal format

There are also 4 methods:
(1) formatDecimal(number,intDig,decDig) The 
first parameter is a single number, the second parameter sets the minimum number of integer digits (if not enough, it will be filled with 0), and the third parameter sets the decimal place Number
(2) arrayFormatDecimal(numArray,intDig,decDig) is 
passed into the array, and the processed array is returned
(3) listFormatDecimal(numList,intDig,decDig) is 
passed into the List, and the processed List is returned
(4)setFormatDecimal(numSet,intDig, decDig) is 
passed in to Set and returned to the processed Set
. There are two overloaded methods for these four methods. Take formatDecimal as an example:
(a)formatDecimal(number,intDig,decDig,decPoint)
decPoint represents what symbol is used as the decimal point, take Values ​​are POINT, COMMA, WHITESPACE, NONE, and DEFAULT.
(b) formatDecimal(number,intDig,separator,decDig,decPoint)
separator indicates what symbol is used as the thousands separator, and the values ​​are also 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

Three, percentage formatting

Similar to the formatting of decimals, there are also 4 methods. Among them,
formatPercent(number,intDig,decDig) is used to process a single number. The 
first parameter is a single number, and the second parameter sets the minimum number of integer digits (if not enough, 0 will be added) , The third parameter is set to retain the number of decimal places

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%

Four, sequence method

The sequence method returns an Integer array.
(1) sequence(from,to)
sets the start value and end value. If from is greater than to, the default step is 1, otherwise it is -1.
(2) sequence (from, to, step)
sets the start value and end value, and the step length.

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:

1
3
----------
4
2

 

Guess you like

Origin blog.csdn.net/gdjlc/article/details/102528509