Java工具类——把金额转换成汉字大写金额


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. class MoneyFormat{    
  2.      private final String [] pattern ={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};    
  3.      private final String [] cPattern ={"","拾","佰","仟","万","拾","佰","仟","亿"};    
  4.      private final String [] cfPattern = {"","角","分"};    
  5.      private final String ZEOR = "零";    
  6.      public MoneyFormat(){    
  7.          System.out.println("run...");    
  8.           
  9.      }     
  10.          
  11.      public String format(String moneyString){    
  12.       int dotPoint = moneyString.indexOf("."); //判断是否为小数    
  13.       String moneyStr;          
  14.       if(dotPoint != -1){    
  15.        moneyStr = moneyString.substring(0,moneyString.indexOf("."));    
  16.       }    
  17.       else{    
  18.        moneyStr = moneyString;    
  19.       }    
  20.       StringBuffer fraction = null;   //小数部分的处理,以及最后的yuan.    
  21.       StringBuffer ms = new StringBuffer();     
  22.       for(int i = 0;i < moneyStr.length();i++){    
  23.        ms.append(pattern[moneyStr.charAt(i) - 48]); //按数组的编号加入对应大写汉字    
  24.       }    
  25.           
  26.       int cpCursor = 1;    
  27.       for(int j = moneyStr.length() - 1;j > 0;j--){    
  28.        ms.insert(j,cPattern[cpCursor]);   //在j之后加字符,不影响j对原字符串的相对位置    
  29.                   //只是moneyStr.length()不断增加    
  30.                   //insert(j,"string")就在j位置处插入,j=0时为第一位    
  31.        cpCursor = cpCursor == 8?1:cpCursor + 1;    //亿位之后重新循环    
  32.       }    
  33.           
  34.           
  35.       while(ms.indexOf("零拾") != -1){  //当十位为零时用一个"零"代替"零拾"    
  36.                 //replace的起始于终止位置    
  37.        ms.replace(ms.indexOf("零拾"),ms.indexOf("零拾") + 2,ZEOR);    
  38.       }    
  39.       while(ms.indexOf("零佰") != -1){  //当百位为零时,同理    
  40.        ms.replace(ms.indexOf("零佰"),ms.indexOf("零佰") + 2,ZEOR);    
  41.       }    
  42.       while(ms.indexOf("零仟") != -1){  //同理    
  43.        ms.replace(ms.indexOf("零仟"),ms.indexOf("零仟") + 2,ZEOR);    
  44.       }    
  45.       while(ms.indexOf("零万") != -1){  //万需保留,中文习惯    
  46.        ms.replace(ms.indexOf("零万"),ms.indexOf("零万") + 2,"万");    
  47.       }    
  48.       while(ms.indexOf("零亿") != -1){  //同上    
  49.        ms.replace(ms.indexOf("零亿"),ms.indexOf("零亿") + 2,"亿");    
  50.       }    
  51.       while(ms.indexOf("零零") != -1){//有连续数位出现零,即有以下情况,此时根据习惯保留一个零即可    
  52.        ms.replace(ms.indexOf("零零"),ms.indexOf("零零") + 2,ZEOR);    
  53.       }    
  54.       while(ms.indexOf("亿万") != -1){  //特殊情况,如:100000000,根据习惯保留高位    
  55.        ms.replace(ms.indexOf("亿万"),ms.indexOf("亿万") + 2,"亿");    
  56.       }    
  57.       while(ms.lastIndexOf("零") == ms.length()-1){  //当结尾为零j,不必显示,经过处理也只可能出现一个零    
  58.           if(ms.indexOf("零") == -1){  
  59.               ms.delete(ms.lastIndexOf("零"),ms.lastIndexOf("零") + 1);    
  60.           }else{  
  61.               break;  
  62.           }  
  63.       }    
  64.           
  65.           
  66.       int end;    
  67.       if((dotPoint = moneyString.indexOf(".")) != -1 ){ //是小数的进入     
  68.        String fs = moneyString.substring(dotPoint + 1,moneyString.length());    
  69.        if(fs.indexOf("00") == -1 || fs.indexOf("00") >= 2){//若前两位小数全为零,则跳过操作    
  70.         end = fs.length() > 2?2:fs.length();  //仅保留两位小数    
  71.         fraction = new StringBuffer(fs.substring(0,end));    
  72.         for(int j = 0;j < fraction.length();j++){    
  73.          fraction.replace(j,j+1,this.pattern[fraction.charAt(j) - 48]); //替换大写汉字    
  74.         }    
  75.         for(int i = fraction.length();i > 0;i--){  //插入中文标识    
  76.          fraction.insert(i,cfPattern[i]);    
  77.         }    
  78.         fraction.insert(0,"元");      //为整数部分添加标识    
  79.        }    
  80.        else{    
  81.         fraction = new StringBuffer("元整");     
  82.        }    
  83.            
  84.       }    
  85.       else{    
  86.        fraction = new StringBuffer("元整");    
  87.       }    
  88.            
  89.       ms.append(fraction);         //加入小数部分    
  90.       return ms.toString();    
  91.      }    
  92.          
  93.          
  94.          
  95.          
  96.      public static void main(String [] ar){    
  97.       System.out.println(new MoneyFormat().format("10005022.123009"));    
  98.       System.out.println(new MoneyFormat().format("0.12"));    
  99.      }    
  100.     }     

转自【http://www.open-open.com/lib/view/open1400206687504.html】
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值