日期:2014-05-18  浏览次数:20762 次

各位大哥,有没写过数字转中文的函数,譬如,把30045转换成三万零肆十伍
如题

------解决方案--------------------
没写过,帮你顶下.
------解决方案--------------------
以前论坛上收集的,希望作者不追究版权

import java.io.*;


public class a
{
public static void main(String[] args)
{
String s=new String();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

while(true)
{
try{s=br.readLine();}catch(Exception e){}
Convert c = new Convert(s.toCharArray());
System.out.println(c);
}
}
}
class Convert
{
final String[] str1={ " ", "十 ", "百 ", "千 "};
final String[] str2 = { "亿 ", "萬 "};
final String[] number = { " ", "壹 ", "贰 ", "叁 ", "肆 ", "伍 ", "陆 ", "柒 ", "捌 ", "玖 "};
char[] num_char;
StringBuffer sb=new StringBuffer();

public Convert(char[] num)
{
num_char=num;
}
public String toString()
{
int last=num_char.length%4;
int pos=0;
add(sb,new String(num_char,0,last));
pos+=last;
for(int i=(int) (num_char.length/4);i> 0;i--)
{
if(i%2==0) sb.append(str2[0]);
else sb.append(str2[1]);
add(sb,new String(num_char,pos,4));
pos+=4;
}
if(last==0) sb.deleteCharAt(0);
return sb.toString();
}
private void add(StringBuffer sb,String temp)
{
char[] temp2=temp.toCharArray();
int length=temp.length();
for(int i=0;i <length;i++)
{
if(temp2[i]== '0 ')
if(sb.lastIndexOf( "零 ")!=sb.length()-1 && i!=0) sb.append( "零 ");else;
else
{
sb.append(number[temp2[i]- '0 ']);
sb.append(str1[length-i-1]);
}
}
try{
if (sb.lastIndexOf( "零 ") == sb.length() - 1) sb.deleteCharAt(sb.lastIndexOf( "零 "));
}catch(Exception e){}
}
}

------解决方案--------------------
magus163(菜鸟怎么赚分?)

----------------------------------
这个我也测试过,不错。
------解决方案--------------------
收藏把 ,
------解决方案--------------------
以前写过,代码找不见了.记得用switch(expression);应该挺简单吧!