/** *//**
* 数字转化成中文输出,如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℃太阳 阅读(281)
评论(1) 编辑 收藏 所属分类:
代码民工