Thymeleaf对象的使用:数字对象

Thymeleaf主要使用 org.thymeleaf.expression.Numbers 类处理数字,在模板中使用 #numbers 对象来处理数字。

开发环境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8

新建一个名称为demo的Spring Boot项目。
pom.xml加入Thymeleaf依赖:

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

一、整数格式化

有4个方法:
(1)formatInteger(number,digits)
第一个参数为单个数字,如果有小数字点则四舍五入,第二个参数设置最少的整数位数,不足会补0(下同)
(2)arrayFormatInteger(numbers,digits)
传入数组,返回处理后的数组
(3)listFormatInteger(numbers,digits)
传入List,返回处理后的List
(4)setFormatInteger(numbers,digits)
传入Set,返回处理后的Set
这4个方法存在重载方法传入第三个参数,用于标识千位分隔符
POINT : 使用“.”
COMMA : 使用“,”
WHITESPACE : 使用“ ”(空格)
NONE : 不使用分隔符
DEFAULT : 根据Locale对象来决定

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";
    }
}

浏览器访问:http://localhost:8080/integer
页面输出:

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

二、小数格式化

同样有4个方法:
(1)formatDecimal(number,intDig,decDig)
第一个参数为单个数字,第二个参数设置最少的整数位数(不足会补0),第三个参数设置保留小数位数
(2)arrayFormatDecimal(numArray,intDig,decDig)
传入数组,返回处理后的数组
(3)listFormatDecimal(numList,intDig,decDig)
传入List,返回处理后的List
(4)setFormatDecimal(numSet,intDig,decDig)
传入Set,返回处理后的Set
这4个方法都存在两个重载方法,以formatDecimal为例:
(a)formatDecimal(number,intDig,decDig,decPoint)
decPoint表示用什么符号作为小数点,取值为POINT、COMMA、WHITESPACE、NONE和DEFAULT。
(b)formatDecimal(number,intDig,separator,decDig,decPoint)
separator表示用什么符号作为千位分隔符,同样取值为POINT、COMMA、WHITESPACE、NONE和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";
    }
}

浏览器访问:http://localhost:8080/decimal
页面输出:

10
10.60
00010.60
10000000,00
10000000.00
10.000.000.00

三、百分比格式化

和小数的格式化类似,同样有4个方法,其中处理单个数字用
formatPercent(number,intDig,decDig)
第一个参数为单个数字,第二个参数设置最少的整数位数(不足会补0),第三个参数设置保留小数位数

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";
    }
}

浏览器访问:http://localhost:8080/percent
页面输出:

12.30%
00,012.30%

四、sequence方法

sequence方法返回Integer数组。
(1)sequence(from,to)
设置开始值与结束值,如果from比to大,则默认步长为1,否则为-1。
(2)sequence(from,to,step)
设置开始值与结束值,步长。

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";
    }
}

浏览器访问:http://localhost:8080/percent
页面输出:

0
1
2
3
----------
5
4
3
2
1
原文地址:https://www.cnblogs.com/gdjlc/p/11664418.html