189 8069 5689

给定数字生成中文数字

  1. public class T{
  2.     public static void main(String[] args) {
  3.         int i=123456789;
  4.         Solution s=new Solution();
  5.         System.out.println(s.intToroman(1234567890));
  6.         System.out.println(s.intToroman(150001));
  7.         System.out.println(s.intToroman(30150000));
  8.     }
  9. }

  10. class Solution{
  11.     String[] metadata=newString[]{"零","一","二","三","四","五","六","七","八","九"};
  12.     String[] bit=newString[]{"","十","百","千","","万","亿"};


  13.     public String intToroman(int i){
  14.         String result="";
  15.         int r=0;
  16.         while(i>0){
  17.             int cur=i%10000;
  18.             i=i/10000;
  19.             boolean flag=cur<1000&&i>0; //计算前面需要补零的情况
  20.             int index=0;
  21.             String s="";
  22.             while(cur>0){
  23.                 s= metadata[cur%10]+ (cur%10==0?"":bit[index]) +s;
  24.                 cur=cur/10;
  25.                 index++;
  26.             }
  27.             //每四位,加单位,补零
  28.             result=(flag?"零":"")+s+bit[4+r]+result;
  29.             r++;
  30.         }
  31.         //如果有多个位都是零,则保留一个.各位的零抹去
  32.         return result.replaceAll("零+","零").replaceAll("零$","");
  33.     }
  34. }

结果:
一十二亿三千四百五十六万七千八百九十
一十五万零一
三千零一十五万


分享文章:给定数字生成中文数字
URL网址:http://cdxtjz.com/article/iggich.html

其他资讯