hjh132's Blog
If a thing is worth doing it is worth doing well.
posts - 20,comments - 9,trackbacks - 0
/**
 * 数字转化成中文输出,如123456789,输出为一亿二千三百四拾五万六千七百八百九
 * 
@author HJH
 * 
@version 1.0,02/26/2008
 
*/

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

    
    
public static String init(String str)
    
{
        str 
= str.replace("1","");
        str 
= str.replace("2","");
        str 
= str.replace("3","");
        str 
= str.replace("4","");
        str 
= str.replace("5","");
        str 
= str.replace("6","");
        str 
= str.replace("7","");
        str 
= str.replace("8","");
        str 
= str.replace("9","");
        str 
= str.replace("0","");

        
return str;
    }

    
    
public static String[] separate(String str)  //将str分成每四个一组
    {
        
int len = str.length();
        
int divInt = len/4//
        int divFloat = len%4//余数
        String[] strArray = new String[divInt + 1];
        
int count = 0;
        
        
for (int i = 0; i < strArray.length; i++)
        
{
            
if (i == 0)
                strArray[i] 
= str.substring(0,divFloat);
            
else
            
{
                strArray[i] 
= str.substring(divFloat + count,divFloat + count + 4);
                count 
+= 4;
            }

        }


        
return strArray;
    }

    
    
public static String readNumArray(String[] str)
    
{
        String temp 
= "";
        String[] chNum 
= new String[]{"亿","",""};
        
for(int i = 0; i < str.length; i++)
        
{
            
if (str.length <= 1)
                temp 
+= readFourNum(str[i]);
            
else if(str.length > 1 && str.length <= 2)
            
{
                temp 
+= readFourNum(str[i]) + chNum[i+1];
            }

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

        
return temp;
    }

    
    
public static String readFourNum(String str)
    
{
        
int len = str.length();
        String[] chNum 
= new String[]{"","","",""};
        String temp 
= "";
        
switch(len)
        
{
        
case 4:
            
for(int i = 0; i < len; i++)
            
{
                temp 
+= str.charAt(i)+  chNum[i];
                
//System.out.print(temp);
            }

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

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

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

            
break;
            
        }

        
        
return temp;
    }

    
    
public static void main(String[] args)
    
{
        String s 
= readNum("1234567890");
        System.out.println(s);
    }

}
posted on 2008-02-26 14:18 101℃太阳 阅读(271) 评论(1)  编辑  收藏 所属分类: 代码民工

FeedBack:
# re: 数字转化成中文
2008-02-29 13:10 | ~!@#$%^&*()_+
昨天我面试也遇到这个的问题,早点看到这文章就好了~~呜呜~~  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: