zoukankan      html  css  js  c++  java
  • Thymeleaf货币转换

    #概述

    本文,将介绍如何使用页面组件Thymeleaf对货币进行自动转换

    #Maven依赖

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

    #创建thymeleaf页面和Controller

    在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>
    

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

    我们需要根据浏览器的区域来转换货币。使用Accept-Language表示用户的区域.在Chrome浏览器里可以设置。

    启动应用后,在浏览器里输入 http://localhost:8080/currency?amount=1000000.01

    返回的结果是:¥1,000,000.01

    如果把浏览的语言设置为English (United States)

    返回的结果是: $1,000,000.01

    #列表或数组

    我们也可以转换一个列表或数组.修改Controller如下:

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

    修改页面如下:

    <!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>
    
    

    在浏览器里输入:http://localhost:8080/currency?amount=1000000.01&amountList=10&amountList=10000&amountList=100000

    返回的结果是:¥1,000,000.01 [¥10.00, ¥10,000.00, ¥100,000.00]

    #尾数零

    尾数是.00,可以使用strings.replace方法删除:

    <h3 th:text="${#strings.replace(#numbers.formatCurrency(param.amount), '.00', '')}"> </h3>
    

    #小数点

    根据不同的区域,小数点的格式可能不同。因此,如果我们想用逗号“,”代替小数点“.”,可以使用Numbers类提供的 formatDecimal方法:

    <h3 th:text="${#numbers.formatDecimal(param.amountList, 1, 2, 'COMMA')}"></h3>
    <h3 th:text="${#numbers.listFormatDecimal(param.amountList, 1, 2, 'COMMA')}"></h3>
    

    #参考资料

    [1]http://deepincoding.com/post/9

     

  • 相关阅读:
    适合于小团队产品迭代的APP测试流程
    【转】软件测试上线标准
    安全性测试之修改密码
    LoadRunner 实现监控Tomcat
    【转】人生应该接受的教育
    晓光聊《小厂如何做测试》
    由测试需要多少编程知识想到的
    12款很棒的浏览器兼容性测试工具推荐
    最近感悟测试人员需要的一种能力
    APP测试功能点总结
  • 原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/12991480.html
Copyright © 2011-2022 走看看