Convert numbers to RMB

  1. public class  MoneyUtil {   
  2.     private final static  String[] CN_Digits = {  "zero" "one" "two" "three" " four" ,  "wu " ,    
  3.             "Lu" "Qi" "Eighth" "Jiu" , };  
  4.   
  5.     /** 
  6.      * Convert digital currency to Chinese currency <br/> 
  7.      * Author: wallimn Time: 2009-4-10 09:59:26 PM<br/> 
  8.      * Blog: http://blog.csdn.net/wallimn<br/> 
  9.      * Parameters: <br/> 
  10.      *  
  11.      * @param moneyValue 
  12.      * The amount in the form of a string, the decimal part, the part with more than 3 digits will be rounded off, and no rounding will be done 
  13.      * @return 
  14.      */  
  15.     publicstatic String CNValueOf(String moneyValue) {   
  16.         //Use regular expressions to remove leading zeros and commas in numbers  
  17.         String value = moneyValue.replaceFirst("^0+""");  
  18.         value = value.replaceAll(",""");  
  19.         // split the fractional part and the integer part  
  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( 0 2 ); //Add two 0s to facilitate unified processing later  
  30.         }  
  31.   
  32.         int len = int_value.length();  
  33.         if  (len> 16 return "value too large" ;   
  34.         StringBuffer cn_currency = new StringBuffer();  
  35.         String[] CN_Carry = new String[] { """万""亿""万" };  
  36.         //Number group processing, count the number of groups  
  37.         int  cnt = len / 4 + (len% 4 == 0 ? 0 : 1 );  
  38.         //The length of the first group on the left  
  39.         int  partLen = len-(cnt- 1 )* 4 ;  
  40.         String partValue=null;  
  41.         boolean  bZero= false ; // has zero crossings  
  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.             //If the last time was zero, this time it is not zero, then add zero            
  49.             if(bZero && !"零".equals(curCN)){  
  50.                 cn_currency.append("零");  
  51.                 bZero= false ;  
  52.             }  
  53.             if("零".equals(curCN))bZero=true;  
  54.             //If the number is not zero, add Chinese numbers and units  
  55.             if(!"零".equals(curCN)){  
  56.                 cn_currency.append(curCN);  
  57.                 cn_currency.append(CN_Carry[cnt-1-i]);  
  58.             }  
  59.             //Except for the leftmost group of indeterminate lengths, all other lengths are 4  
  60.             partLen = 4 ;  
  61.             partValue = null ;  
  62.         }  
  63.         cn_currency.append("元");  
  64.         // handle the fractional part  
  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.      * Convert a group of numbers (no more than four) into Chinese representation <br/> 
  79.      * Author: wallimn Time: 2009-4-11 07:41:25 PM<br/> 
  80.      * 博客:http://wallimn.iteye.com<br/> 
  81.      * Parameters: <br/> 
  82.      *  
  83.      * @param partValue number as string 
  84.      * @param bInsertZero whether to prepend zeros 
  85.      * @return 
  86.      */  
  87.     privatestatic String Part2CN(String partValue,boolean bInsertZero) {   
  88.         //Use a regular expression to remove the leading 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.                 // If it is not the last digit, and the next digit is not zero, append zero  
  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.     }  

Note: The above code is not written by me, but I saw it in a blog. I hope to share it with you.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326525161&siteId=291194637