- public class MoneyUtil {
- private final static String[] CN_Digits = { "zero" , "one" , "two" , "three" , " four" , "wu " ,
- "Lu" , "Qi" , "Eighth" , "Jiu" , };
- /**
- * Convert digital currency to Chinese currency <br/>
- * Author: wallimn Time: 2009-4-10 09:59:26 PM<br/>
- * Blog: http://blog.csdn.net/wallimn<br/>
- * Parameters: <br/>
- *
- * @param moneyValue
- * 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
- * @return
- */
- publicstatic String CNValueOf(String moneyValue) {
- //Use regular expressions to remove leading zeros and commas in numbers
- String value = moneyValue.replaceFirst("^0+", "");
- value = value.replaceAll(",", "");
- // split the fractional part and the integer part
- int dot_pos = value.indexOf('.');
- String int_value;
- String fraction_value;
- if (dot_pos == -1) {
- int_value = value;
- fraction_value = "00";
- } else {
- int_value = value.substring(0, dot_pos);
- fraction_value = value.substring(dot_pos + 1, value.length())
- + "00" .substring( 0 , 2 ); //Add two 0s to facilitate unified processing later
- }
- int len = int_value.length();
- if (len> 16 ) return "value too large" ;
- StringBuffer cn_currency = new StringBuffer();
- String[] CN_Carry = new String[] { "", "万", "亿", "万" };
- //Number group processing, count the number of groups
- int cnt = len / 4 + (len% 4 == 0 ? 0 : 1 );
- //The length of the first group on the left
- int partLen = len-(cnt- 1 )* 4 ;
- String partValue=null;
- boolean bZero= false ; // has zero crossings
- String curCN=null;
- for(int i =0; i<cnt; i++){
- partValue = int_value.substring(0,partLen);
- int_value=int_value.substring(partLen);
- curCN = Part2CN(partValue,i!=0&&!"零".equals(curCN));
- //System.out.println(partValue+":"+curCN);
- //If the last time was zero, this time it is not zero, then add zero
- if(bZero && !"零".equals(curCN)){
- cn_currency.append("零");
- bZero= false ;
- }
- if("零".equals(curCN))bZero=true;
- //If the number is not zero, add Chinese numbers and units
- if(!"零".equals(curCN)){
- cn_currency.append(curCN);
- cn_currency.append(CN_Carry[cnt-1-i]);
- }
- //Except for the leftmost group of indeterminate lengths, all other lengths are 4
- partLen = 4 ;
- partValue = null ;
- }
- cn_currency.append("元");
- // handle the fractional part
- int fv1 = Integer.parseInt(fraction_value.substring(0,1));
- int fv2 = Integer.parseInt(fraction_value.substring(1,2));
- if(fv1+fv2==0){
- cn_currency.append("整");
- }
- else{
- cn_currency.append(CN_Digits[fv1]).append("角");
- cn_currency.append(CN_Digits[fv2]).append("分");
- }
- return cn_currency.toString();
- }
- /**
- * Convert a group of numbers (no more than four) into Chinese representation <br/>
- * Author: wallimn Time: 2009-4-11 07:41:25 PM<br/>
- * 博客:http://wallimn.iteye.com<br/>
- * Parameters: <br/>
- *
- * @param partValue number as string
- * @param bInsertZero whether to prepend zeros
- * @return
- */
- privatestatic String Part2CN(String partValue,boolean bInsertZero) {
- //Use a regular expression to remove the leading 0
- partValue = partValue.replaceFirst("^0+", "");
- int len = partValue.length();
- if (len == 0)
- return"零";
- StringBuffer sbResult = new StringBuffer();
- int digit;
- String[] CN_Carry = new String[] { "", "拾", "佰", "仟" };
- for (int i = 0; i < len; i++) {
- digit = Integer.parseInt(partValue.substring(i, i + 1));
- if (digit != 0) {
- sbResult.append(CN_Digits[digit]);
- sbResult.append(CN_Carry[len - 1 - i]);
- } else {
- // If it is not the last digit, and the next digit is not zero, append zero
- if (i != len - 1
- && Integer.parseInt(partValue.substring(i + 1, i + 2)) != 0)
- sbResult.append("零");
- }
- }
- if(bInsertZero && len!=4)sbResult.insert(0, "零");
- return sbResult.toString();
- }
- }
Note: The above code is not written by me, but I saw it in a blog. I hope to share it with you.