1 /** 3  * @description  金额元分之间转换工具类 6  */
 7 public class AmountUtils {
 8 
 9     /**金额为分的格式 */
10     public static final String CURRENCY_FEN_REGEX = "\\-?[0-9]+";
11 
12     /**
13      * 将分为单位的转换为元并返回金额格式的字符串 (除100)
14      *
15      * @param amount
16      * @return
17      * @throws Exception
18      */
19     public static String changeF2Y(Long amount) throws Exception{
20         if(!amount.toString().matches(CURRENCY_FEN_REGEX)) {
21             throw new Exception("金额格式有误");
22         }
23 
24         int flag = 0;
25         String amString = amount.toString();
26         if(amString.charAt(0)==\'-\'){
27             flag = 1;
28             amString = amString.substring(1);
29         }
30         StringBuffer result = new StringBuffer();
31         if(amString.length()==1){
32             result.append("0.0").append(amString);
33         }else if(amString.length() == 2){
34             result.append("0.").append(amString);
35         }else{
36             String intString = amString.substring(0,amString.length()-2);
37             for(int i=1; i<=intString.length();i++){
38                 if( (i-1)%3 == 0 && i !=1){
39                     result.append(",");
40                 }
41                 result.append(intString.substring(intString.length()-i,intString.length()-i+1));
42             }
43             result.reverse().append(".").append(amString.substring(amString.length()-2));
44         }
45         if(flag == 1){
46             return "-"+result.toString();
47         }else{
48             return result.toString();
49         }
50     }
51 
52     /**
53      * 将分为单位的转换为元 (除100)
54      *
55      * @param amount
56      * @return
57      * @throws Exception
58      */
59     public static String changeF2Y(String amount) throws Exception{
60         if(!amount.matches(CURRENCY_FEN_REGEX)) {
61             throw new Exception("金额格式有误");
62         }
63         return BigDecimal.valueOf(Long.parseLong(amount)).divide(new BigDecimal(100)).toString();
64     }
65 
66     /**
67      * 将元为单位的转换为分 (乘100)
68      *
69      * @param amount
70      * @return
71      */
72     public static String changeY2F(Long amount){
73         return BigDecimal.valueOf(amount).multiply(new BigDecimal(100)).toString();
74     }
75 
76     /**
77      * 将元为单位的转换为分 替换小数点,支持以逗号区分的金额
78      *
79      * @param amount
80      * @return
81      */
82     public static String changeY2F(String amount){
83         String currency =  amount.replaceAll("\\$|\\¥|\\,", "");  //处理包含, ¥ 或者$的金额
84         int index = currency.indexOf(".");
85         int length = currency.length();
86         Long amLong = 0l;
87         if(index == -1){
88             amLong = Long.valueOf(currency+"00");
89         }else if(length - index >= 3){
90             amLong = Long.valueOf((currency.substring(0, index+3)).replace(".", ""));
91         }else if(length - index == 2){
92             amLong = Long.valueOf((currency.substring(0, index+2)).replace(".", "")+0);
93         }else{
94             amLong = Long.valueOf((currency.substring(0, index+1)).replace(".", "")+"00");
95         }
96         return amLong.toString();
97     }
98 
99 }

 

版权声明:本文为lizhen-home原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/lizhen-home/p/7692379.html