zoukankan      html  css  js  c++  java
  • 金额元分之间转换工具类

     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 }
  • 相关阅读:
    【连载3】二手电商的交易模式与业务价值
    【连载0】二手电商专题研究前言
    【连载2】二手电商平台的四要素
    【连载1】何为二手实物电商
    阿里的社区梦 能靠闲鱼完成吗?
    二手回收能否翻过BAT这座大山?
    把握这两点,抢占下一个电商风口|2016最新中国电商App排名&研究报告
    互联网巨头为何偏爱“二手货”?告诉你真实的理由
    2016中国app年度排行榜:十大行业、25个领域、Top 500 和2017趋势预测
    Eventbus 使用方法和原理分析
  • 原文地址:https://www.cnblogs.com/lizhen-home/p/7692379.html
Copyright © 2011-2022 走看看