给定数字生成中文数字
-
public class T{
-
public static void main(String[] args) {
-
int i=123456789;
-
Solution s=new Solution();
-
System.out.println(s.intToroman(1234567890));
-
System.out.println(s.intToroman(150001));
-
System.out.println(s.intToroman(30150000));
-
}
-
}
-
-
class Solution{
-
String[] metadata=newString[]{"零","一","二","三","四","五","六","七","八","九"};
-
String[] bit=newString[]{"","十","百","千","","万","亿"};
-
-
-
public String intToroman(int i){
-
String result="";
-
int r=0;
-
while(i>0){
-
int cur=i%10000;
-
i=i/10000;
-
boolean flag=cur<1000&&i>0; //计算前面需要补零的情况
-
int index=0;
-
String s="";
-
while(cur>0){
-
s= metadata[cur%10]+ (cur%10==0?"":bit[index]) +s;
-
cur=cur/10;
-
index++;
-
}
-
//每四位,加单位,补零
-
result=(flag?"零":"")+s+bit[4+r]+result;
-
r++;
-
}
-
//如果有多个位都是零,则保留一个.各位的零抹去
-
return result.replaceAll("零+","零").replaceAll("零$","");
-
}
- }
结果:
一十二亿三千四百五十六万七千八百九十
一十五万零一
三千零一十五万
网页名称:给定数字生成中文数字
网页链接:http://scgulin.cn/article/iggich.html