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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM