TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
public class Test{
public static String numtochinese(String input){
String s1="零壹贰叁肆伍陆柒捌玖";
String s4="分角整元拾佰仟万拾佰仟亿拾佰仟";
String temp="";
String result="";
if (input==null)
return "输入字串不是数字串只能包括以下字符(Ɔ"~Ə","."),输入字串最大只能精确到仟亿,小数点只能两位!";
temp=input.trim();
float f;
try{
f=Float.parseFloat(temp);
}catch(Exception e){
return "输入字串不是数字串只能包括以下字符(Ɔ"~Ə","."),输入字串最大只能精确到仟亿,小数点只能两位!";
}
int len=0;
if (temp.indexOf(".")==-1) len=temp.length();
else len=temp.indexOf(".");
if(len>s4.length()-3) return("输入字串最大只能精确到仟亿,小数点只能两位!");
int n1,n2=0;
String num="";
String unit="";
for(int i=0;i<temp.length();i++){
if(i>len+2){break;}
if(i==len) {continue;}
n1=Integer.parseInt(String.valueOf(temp.charAt(i)));
num=s1.substring(n1,n1+1);
n1=len-i+2;
unit=s4.substring(n1,n1+1);
result=result.concat(num).concat(unit);
}
if ((len==temp.length())||(len==temp.length()-1)) result=result.concat("整");
if (len==temp.length()-2) result=result.concat("零分");
return result;
}
public static void main(String args[]){
String s=Test.numtochinese("54564334567.23");
String s1=Test.numtochinese("5000400003.45");
System.out.println("54564334567.23="+s);
System.out.println("5000400003.45="+s1);
}
}
程序运行结果:
C:java>java Test
54564334567.23=伍佰肆拾伍亿陆仟肆佰叁拾叁万肆仟伍佰陆拾柒元贰角叁分
5000400003.45=伍拾零亿零仟零佰肆拾零万零仟零佰零拾叁元肆角伍分
C:java> |
|