#Overview
This article describes how to use the page component, Thymeleaf, to automatically convert currencies
#Maven depends on
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>2.3.0.RELEASE</version> </dependency>
#Create thmeleaf page and Controller
Create a page under resources/templates / currencies.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Currency Format</title> </head> <body> <h3 th:text="${#numbers.formatCurrency(param.amount)}"></h3> </body> </html>
establish CurrencyController.java
package com.deepincoding.currencyformat; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class CurrencyController { @GetMapping("/currency") public String current(@RequestParam(value = "amount") String amount){ return "currency"; } }
We need to convert currencies based on the browser's region. Use accept language to represent the user's region. It can be set in Chrome browser.
After launching the application, enter it in the browser http://localhost:8080/currency?amount=1000000.01
The returned result is: ¥ 1000000.01
If the browsing language is set to English (United States)
The result returned is: $1000000.01
#List or array
We can also convert a list or array. Modify the Controller as follows:
package com.deepincoding.currencyformat; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import java.util.List; @Controller public class CurrencyController { @GetMapping("/currency") public String current(@RequestParam(value = "amount") String amount, @RequestParam(value = "amountList") List amountList){ return "currency"; } }
The modification page is as follows:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Currency Format</title> </head> <body> <h3 th:text="${#numbers.formatCurrency(param.amount)}"></h3> <h3 th:text="${#numbers.listFormatCurrency(param.amountList)}"></h3> </body> </html>
Enter in the browser: http://localhost:8080/currency?amount=1000000.01&amountList=10&amountList=10000&amountList=100000
The returned result is: ¥ 1000000.01 [¥ 10.00, ¥ 10000.00, ¥ 100000.00]
#Mantissa zero
The mantissa is. 00, which can be used strings.replace Method delete:
<h3 th:text="${#strings.replace(#numbers.formatCurrency(param.amount), '.00', '')}"> </h3>
#Decimal point
Depending on the region, the format of the decimal point may vary. Therefore, if we want to use comma "," instead of decimal point ".", we can use formatDecimal method provided by Numbers class:
<h3 th:text="${#numbers.formatDecimal(param.amountList, 1, 2, 'COMMA')}"></h3> <h3 th:text="${#numbers.listFormatDecimal(param.amountList, 1, 2, 'COMMA')}"></h3>