Springboot Thymeleaf數(shù)字對象使用方法
Thymeleaf主要使用 org.thymeleaf.expression.Numbers 類處理數(shù)字,在模板中使用 #numbers 對象來處理數(shù)字。
開發(fā)環(huán)境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8
新建一個(gè)名稱為demo的Spring Boot項(xiàng)目。
pom.xml加入Thymeleaf依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
一、整數(shù)格式化
有4個(gè)方法:
(1)formatInteger(number,digits)
第一個(gè)參數(shù)為單個(gè)數(shù)字,如果有小數(shù)字點(diǎn)則四舍五入,第二個(gè)參數(shù)設(shè)置最少的整數(shù)位數(shù),不足會(huì)補(bǔ)0(下同)
(2)arrayFormatInteger(numbers,digits)
傳入數(shù)組,返回處理后的數(shù)組
(3)listFormatInteger(numbers,digits)
傳入List,返回處理后的List
(4)setFormatInteger(numbers,digits)
傳入Set,返回處理后的Set
這4個(gè)方法存在重載方法傳入第三個(gè)參數(shù),用于標(biāo)識(shí)千位分隔符
- POINT : 使用“.”
- COMMA : 使用“,”
- WHITESPACE : 使用“ ”(空格)
- NONE : 不使用分隔符
- DEFAULT : 根據(jù)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
二、小數(shù)格式化
同樣有4個(gè)方法:
(1)formatDecimal(number,intDig,decDig)
第一個(gè)參數(shù)為單個(gè)數(shù)字,第二個(gè)參數(shù)設(shè)置最少的整數(shù)位數(shù)(不足會(huì)補(bǔ)0),第三個(gè)參數(shù)設(shè)置保留小數(shù)位數(shù)
(2)arrayFormatDecimal(numArray,intDig,decDig)
傳入數(shù)組,返回處理后的數(shù)組
(3)listFormatDecimal(numList,intDig,decDig)
傳入List,返回處理后的List
(4)setFormatDecimal(numSet,intDig,decDig)
傳入Set,返回處理后的Set
這4個(gè)方法都存在兩個(gè)重載方法,以formatDecimal為例:
(a)formatDecimal(number,intDig,decDig,decPoint)
decPoint表示用什么符號(hào)作為小數(shù)點(diǎn),取值為POINT、COMMA、WHITESPACE、NONE和DEFAULT。
(b)formatDecimal(number,intDig,separator,decDig,decPoint)
separator表示用什么符號(hào)作為千位分隔符,同樣取值為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
三、百分比格式化
和小數(shù)的格式化類似,同樣有4個(gè)方法,其中處理單個(gè)數(shù)字用
formatPercent(number,intDig,decDig)
第一個(gè)參數(shù)為單個(gè)數(shù)字,第二個(gè)參數(shù)設(shè)置最少的整數(shù)位數(shù)(不足會(huì)補(bǔ)0),第三個(gè)參數(shù)設(shè)置保留小數(shù)位數(shù)
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數(shù)組。
(1)sequence(from,to)
設(shè)置開始值與結(jié)束值,如果from比to大,則默認(rèn)步長為1,否則為-1。
(2)sequence(from,to,step)
設(shè)置開始值與結(jié)束值,步長。
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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot如何使用thymeleaf模板訪問html頁面
- 詳解SpringBoot+Thymeleaf 基于HTML5的現(xiàn)代模板引擎
- springboot用thymeleaf模板的paginate分頁完整代碼
- Springboot Thymeleaf實(shí)現(xiàn)HTML屬性設(shè)置
- SpringBoot+mybatis+thymeleaf實(shí)現(xiàn)登錄功能示例
- Spring boot+mybatis+thymeleaf 實(shí)現(xiàn)登錄注冊增刪改查功能的示例代碼
- springboot+thymeleaf+druid+mybatis 多模塊實(shí)現(xiàn)用戶登錄功能
相關(guān)文章
java實(shí)現(xiàn)AES 32位加密解密的方案
Oracle在其官方網(wǎng)站上提供了無政策限制權(quán)限文件(Unlimited Strength Jurisdiction Policy Files),我們只需要將其部署在JRE環(huán)境中,就可以解決限制問題,下面給大家介紹下java實(shí)現(xiàn)AES 32位加密解密的方案,感興趣的朋友一起看看吧2021-11-11Eclipse項(xiàng)目出現(xiàn)紅色嘆號(hào)的解決方法
eclipse工程前面出現(xiàn)紅色嘆號(hào)都是由于eclipse項(xiàng)目、eclipse工程中,缺少了一些jar包等文件引起的,這篇文章主要給大家介紹了關(guān)于Eclipse項(xiàng)目出現(xiàn)紅色嘆號(hào)的解決方法,需要的朋友可以參考下2023-11-11JavaMail整合Spring實(shí)現(xiàn)郵件發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了JavaMail整合Spring實(shí)現(xiàn)郵件發(fā)送功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08Java?8函數(shù)式接口之BinaryOperator使用示例詳解
這篇文章主要大家介紹了Java?8函數(shù)式接口之BinaryOperator,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Java編程實(shí)現(xiàn)服務(wù)器端支持?jǐn)帱c(diǎn)續(xù)傳的方法(可支持快車、迅雷)
這篇文章主要介紹了Java編程實(shí)現(xiàn)服務(wù)器端支持?jǐn)帱c(diǎn)續(xù)傳的方法,涉及Java文件傳輸?shù)南嚓P(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11java實(shí)現(xiàn)批量導(dǎo)入Excel表格數(shù)據(jù)到數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)批量導(dǎo)入Excel表格數(shù)據(jù)到數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08