将数字转换成人民币

  1. public class MoneyUtil {  
  2.     private final static String[] CN_Digits = { "零""壹""貳""叁""肆""伍",  
  3.             "陆""柒""捌""玖", };  
  4.   
  5.     /** 
  6.      * 将数字型货币转换为中文型货币 <br/> 
  7.      * 作者:wallimn 时间:2009-4-10 下午09:59:26<br/> 
  8.      * 博客:http://blog.csdn.net/wallimn<br/> 
  9.      * 参数:<br/> 
  10.      *  
  11.      * @param moneyValue 
  12.      *             字符串形式的金额,小数部分,将多于3位部分舍去,不做四舍五入 
  13.      * @return 
  14.      */  
  15.     public static String CNValueOf(String moneyValue) {  
  16.         //使用正则表达式,去除前面的零及数字中的逗号  
  17.         String value = moneyValue.replaceFirst("^0+""");  
  18.         value = value.replaceAll(",""");  
  19.         //分割小数部分与整数部分  
  20.         int dot_pos = value.indexOf('.');  
  21.         String int_value;  
  22.         String fraction_value;  
  23.         if (dot_pos == -1) {  
  24.             int_value = value;  
  25.             fraction_value = "00";  
  26.         } else {  
  27.             int_value = value.substring(0, dot_pos);  
  28.             fraction_value = value.substring(dot_pos + 1, value.length())  
  29.                     + "00".substring(02);//也加两个0,便于后面统一处理  
  30.         }  
  31.   
  32.         int len = int_value.length();  
  33.         if (len>16return "值过大";  
  34.         StringBuffer cn_currency = new StringBuffer();  
  35.         String[] CN_Carry = new String[] { """万""亿""万" };  
  36.         //数字分组处理,计数组数  
  37.         int cnt = len/4+(len%4==0?0:1);  
  38.         //左边第一组的长度  
  39.         int partLen = len-(cnt-1)*4;  
  40.         String partValue=null;  
  41.         boolean bZero=false;//有过零  
  42.         String curCN=null;  
  43.         for(int i =0; i<cnt; i++){  
  44.             partValue = int_value.substring(0,partLen);  
  45.             int_value=int_value.substring(partLen);  
  46.             curCN = Part2CN(partValue,i!=0&&!"零".equals(curCN));  
  47.             //System.out.println(partValue+":"+curCN);  
  48.             //若上次为零,这次不为零,则加入零            
  49.             if(bZero && !"零".equals(curCN)){  
  50.                 cn_currency.append("零");  
  51.                 bZero=false;  
  52.             }  
  53.             if("零".equals(curCN))bZero=true;  
  54.             //若数字不是零,加入中文数字及单位  
  55.             if(!"零".equals(curCN)){  
  56.                 cn_currency.append(curCN);  
  57.                 cn_currency.append(CN_Carry[cnt-1-i]);  
  58.             }  
  59.             //除最左边一组长度不定外,其它长度都为4  
  60.             partLen=4;  
  61.             partValue=null;  
  62.         }  
  63.         cn_currency.append("元");  
  64.         //处理小数部分  
  65.         int fv1 = Integer.parseInt(fraction_value.substring(0,1));  
  66.         int fv2 = Integer.parseInt(fraction_value.substring(1,2));  
  67.         if(fv1+fv2==0){  
  68.             cn_currency.append("整");  
  69.         }  
  70.         else{  
  71.             cn_currency.append(CN_Digits[fv1]).append("角");  
  72.             cn_currency.append(CN_Digits[fv2]).append("分");  
  73.         }  
  74.         return cn_currency.toString();  
  75.     }  
  76.   
  77.     /** 
  78.      * 将一组数字(不多于四个)转化成中文表示 <br/> 
  79.      * 作者:wallimn 时间:2009-4-11 下午07:41:25<br/> 
  80.      * 博客:http://wallimn.iteye.com<br/> 
  81.      * 参数:<br/> 
  82.      *  
  83.      * @param partValue 字符串形式的数字 
  84.      * @param bInsertZero 是否在前面添加零 
  85.      * @return 
  86.      */  
  87.     private static String Part2CN(String partValue,boolean bInsertZero) {  
  88.         //使用正则表达式,去除前面的0  
  89.         partValue = partValue.replaceFirst("^0+""");  
  90.         int len = partValue.length();  
  91.         if (len == 0)  
  92.             return "零";  
  93.         StringBuffer sbResult = new StringBuffer();  
  94.         int digit;  
  95.         String[] CN_Carry = new String[] { """拾""佰""仟" };  
  96.         for (int i = 0; i < len; i++) {  
  97.             digit = Integer.parseInt(partValue.substring(i, i + 1));  
  98.             if (digit != 0) {  
  99.                 sbResult.append(CN_Digits[digit]);  
  100.                 sbResult.append(CN_Carry[len - 1 - i]);  
  101.             } else {  
  102.                 // 若不是最后一位,且下不位不为零,追加零  
  103.                 if (i != len - 1  
  104.                         && Integer.parseInt(partValue.substring(i + 1, i + 2)) != 0)  
  105.                     sbResult.append("零");  
  106.             }  
  107.         }  
  108.         if(bInsertZero && len!=4)sbResult.insert(0"零");  
  109.         return sbResult.toString();  
  110.     }  

注:上面的代码不是我自己写的,是在一篇博客中看到的,希望分享给大家。

猜你喜欢

转载自dj111111.iteye.com/blog/2100819