欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot Thymeleaf數(shù)字對象使用方法

 更新時(shí)間:2020年04月23日 15:12:23   作者:gdjlc  
這篇文章主要介紹了Springboot Thymeleaf數(shù)字對象使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論