
 /** *//**
/** *//**
 * 数字转化成中文输出,如123456789,输出为一亿二千三百四拾五万六千七百八百九
 * 数字转化成中文输出,如123456789,输出为一亿二千三百四拾五万六千七百八百九
 * @author HJH
 * @author HJH
 * @version 1.0,02/26/2008
 * @version 1.0,02/26/2008
 */
 */
 public class ReadNumber
public class ReadNumber


 {
{
 public static String readNum(String str)
    public static String readNum(String str)

 
     {
{
 String temp = init(str);
        String temp = init(str);
 String[] strArray = separate(temp);
        String[] strArray = separate(temp);
 String tempStr = readNumArray(strArray);
        String tempStr = readNumArray(strArray);
 
        
 return tempStr;
        return tempStr;
 }
    }
 
    
 public static String init(String str)
    public static String init(String str)

 
     {
{
 str = str.replace("1","一");
        str = str.replace("1","一");
 str = str.replace("2","二");
        str = str.replace("2","二");
 str = str.replace("3","三");
        str = str.replace("3","三");
 str = str.replace("4","四");
        str = str.replace("4","四");
 str = str.replace("5","五");
        str = str.replace("5","五");
 str = str.replace("6","六");
        str = str.replace("6","六");
 str = str.replace("7","七");
        str = str.replace("7","七");
 str = str.replace("8","八");
        str = str.replace("8","八");
 str = str.replace("9","九");
        str = str.replace("9","九");
 str = str.replace("0","零");
        str = str.replace("0","零");

 return str;
        return str;
 }
    }
 
    
 public static String[] separate(String str)  //将str分成每四个一组
    public static String[] separate(String str)  //将str分成每四个一组

 
     {
{
 int len = str.length();
        int len = str.length();
 int divInt = len/4; //商
        int divInt = len/4; //商
 int divFloat = len%4; //余数
        int divFloat = len%4; //余数
 String[] strArray = new String[divInt + 1];
        String[] strArray = new String[divInt + 1];
 int count = 0;
        int count = 0;
 
        
 for (int i = 0; i < strArray.length; i++)
        for (int i = 0; i < strArray.length; i++)

 
         {
{
 if (i == 0)
            if (i == 0)
 strArray[i] = str.substring(0,divFloat);
                strArray[i] = str.substring(0,divFloat);
 else
            else

 
             {
{
 strArray[i] = str.substring(divFloat + count,divFloat + count + 4);
                strArray[i] = str.substring(divFloat + count,divFloat + count + 4);
 count += 4;
                count += 4;
 }
            }
 }
        }

 return strArray;
        return strArray;
 }
    }
 
    
 public static String readNumArray(String[] str)
    public static String readNumArray(String[] str)

 
     {
{
 String temp = "";
        String temp = "";

 String[] chNum = new String[]
        String[] chNum = new String[] {"亿","万",""};
{"亿","万",""};
 for(int i = 0; i < str.length; i++)
        for(int i = 0; i < str.length; i++)

 
         {
{
 if (str.length <= 1)
            if (str.length <= 1)
 temp += readFourNum(str[i]);
                temp += readFourNum(str[i]);
 else if(str.length > 1 && str.length <= 2)
            else if(str.length > 1 && str.length <= 2)

 
             {
{
 temp += readFourNum(str[i]) + chNum[i+1];
                temp += readFourNum(str[i]) + chNum[i+1];
 }
            }
 else if(str.length > 2 && str.length <= 3)
            else if(str.length > 2 && str.length <= 3)
 temp += readFourNum(str[i]) + chNum[i];
                temp += readFourNum(str[i]) + chNum[i];
 else
            else 
 return "ERROR";
                return "ERROR";
 }
        }
 return temp;
        return temp;
 }
    }
 
    
 public static String readFourNum(String str)
    public static String readFourNum(String str)

 
     {
{
 int len = str.length();
        int len = str.length();

 String[] chNum = new String[]
        String[] chNum = new String[] {"千","百","拾",""};
{"千","百","拾",""};
 String temp = "";
        String temp = "";
 switch(len)
        switch(len)

 
         {
{
 case 4:
        case 4:
 for(int i = 0; i < len; i++)
            for(int i = 0; i < len; i++)

 
             {
{
 temp += str.charAt(i)+  chNum[i];
                temp += str.charAt(i)+  chNum[i];
 //System.out.print(temp);
                //System.out.print(temp);
 }
            }
 break;
            break;
 
            
 case 3:
        case 3:
 for(int i = 0; i < len; i++)
            for(int i = 0; i < len; i++)

 
             {
{
 temp += str.charAt(i) + chNum[i + 1];
                temp += str.charAt(i) + chNum[i + 1];
 }
            }
 break;
            break;
 case 2:
        case 2:
 for(int i = 0; i < len; i++)
            for(int i = 0; i < len; i++)

 
             {
{
 temp += str.charAt(i) + chNum[i + 2];
                temp += str.charAt(i) + chNum[i + 2];
 }
            }
 break;
            break;
 case 1:
        case 1:
 for(int i = 0; i < len; i++)
            for(int i = 0; i < len; i++)

 
             {
{
 temp += str.charAt(i) + chNum[i + 3];
                temp += str.charAt(i) + chNum[i + 3];
 }
            }
 break;
            break;
 
            
 }
        }
 
        
 return temp;
        return temp;
 }
    }
 
    
 public static void main(String[] args)
    public static void main(String[] args)

 
     {
{
 String s = readNum("1234567890");
        String s = readNum("1234567890");
 System.out.println(s);
        System.out.println(s);
 }
    }
 }
}posted on 2008-02-26 14:18 
101℃太阳 阅读(306) 
评论(1)  编辑  收藏  所属分类: 
代码民工