黑马程序员技术交流社区

标题: 练习:金额转换,阿拉伯数字转换成中文传统形式 [打印本页]

作者: fmi110    时间: 2015-9-5 12:33
标题: 练习:金额转换,阿拉伯数字转换成中文传统形式
a
  1. import java.util.Scanner;


  2. public class Test2 {

  3.         public static void main(String[] args) {
  4.                 // TODO Auto-generated method stub
  5.                 long num = 101000001010l;//一千零一十亿零一千零一十圆整
  6.                 num = 1010l;//一千零一十亿零一千零一十圆整
  7.                 while(true){
  8.                        
  9.                         System.out.println("输入数学数字:");
  10.                         num = new Scanner(System.in).nextLong();
  11.                         String[] unit = {"圆整","万","亿"};
  12.                         int count = -1;
  13.                         StringBuilder sb = new StringBuilder("");
  14.                        
  15.                         while(num>0){
  16.                                 count++;
  17.                                 String s = "";
  18.                                 long n = num%10000;//取后四位
  19.                                
  20.                                 if(n < 10)
  21.                                         s = "000"+n;
  22.                                 else if(n < 100 )
  23.                                         s = "00"+n;
  24.                                 else if(n < 1000)
  25.                                         s = "0"+n;   
  26.                                 else
  27.                                         s = s+n;
  28.                                 num /= 10000;
  29.                                 if(!s.equals("0000"))
  30.                                         s = toNum(s)+unit[count];//返回的数字加单位  
  31.                                 else
  32.                                         s = toNum(s);
  33.                                 sb.insert(0, s);
  34. //                        System.out.println(sb);
  35.                         }
  36.                         String chineseNum = sb.toString();
  37.                         System.out.println(chineseNum);
  38.                 }

  39.         }
  40.         public static String toNum(String num){
  41.                 String[] digit =        {"零","壹","貳","叁","肆","伍","陆","柒","扒","玖"};
  42.                 char[] ch = num.toCharArray();//字符串形式的四位数
  43.                
  44.                 int count = 0;//记录字符串含0的个数
  45.                 int index = -1;
  46.                 while((index = num.indexOf("0",index+1))!= -1)
  47.                         count++;
  48. //                System.out.println("字符串含0个数count = "+count);
  49.                 String numStr = "";//返回的中文数字
  50.                
  51.                 switch(count){
  52.                         case 4:
  53.                                 numStr = "零";
  54.                                 break;
  55.                         case 3:
  56.                                 int temp = Integer.parseInt(num);
  57.                                 if(temp<10)
  58.                                         numStr = digit[ch[3]-'0'];
  59.                                 else if(temp > 9 && temp < 101)
  60.                                         numStr = digit[ch[2]-'0']+"拾";
  61.                                 else if(temp > 99 && temp < 1000)
  62.                                         numStr = digit[ch[1]-'0'] + "百";
  63.                                 else
  64.                                         numStr = digit[ch[0]-'0'] + "仟";
  65.                                 break;
  66.                         case 2:
  67.                                 if(num.matches("[0][0][1-9][1-9]")){
  68.                                         numStr = numStr + digit[ch[2]-'0']+"拾"+digit[ch[3]-'0'];;
  69.                                 }
  70.                                 else if(num.matches("[1-9][0][0][1-9]")){
  71.                                         numStr = numStr + digit[ch[0]-'0'] + "仟" +"零"+ digit[ch[3]-'0'];
  72.                                 }
  73.                                 else if(num.matches("[1-9][1-9][0][0]")){
  74.                                         numStr = numStr + digit[ch[0]-'0'] + "仟" +digit[ch[1]-'0'] + "百";
  75.                                 }
  76.                                 else if(num.matches("[0][1-9][1-9][0]")){
  77.                                         numStr = numStr + digit[ch[1]-'0'] + "百"+ digit[ch[2]-'0']+"拾";
  78.                                 }
  79.                                 else if(num.matches("[0][1-9][0][1-9]")){
  80.                                         numStr = numStr + digit[ch[1]-'0'] + "百"+"零"+ digit[ch[3]-'0'];
  81.                                 }
  82.                                 else if(num.matches("[1-9][0][1-9][0]")){
  83.                                         numStr = numStr + digit[ch[0]-'0'] + "仟"+"零"+ digit[ch[2]-'0']+"拾";
  84.                                 }
  85.                                 break;
  86.                         case 1:
  87.                                 if(num.matches("[0][1-9][1-9][1-9]")){
  88.                                         numStr = numStr + digit[ch[1]-'0'] + "百" + digit[ch[2]-'0']+"拾"+digit[ch[3]-'0'];
  89.                                 }
  90.                                 else if(num.matches("[1-9][0][1-9][1-9]")){
  91.                                         numStr = numStr + digit[ch[0]-'0'] + "仟" +"零"+  digit[ch[2]-'0']+"拾"+digit[ch[3]-'0'];
  92.                                 }
  93.                                 else if(num.matches("[1-9][1-9][0][1-9]")){
  94.                                         numStr = numStr + digit[ch[0]-'0'] + "仟" +digit[ch[1]-'0'] + "百" + "零"+digit[ch[3]-'0'];
  95.                                 }
  96.                                 else if(num.matches("[1-9][1-9][1-9][0]")){
  97.                                         numStr = numStr + digit[ch[0]-'0'] + "仟" +digit[ch[1]-'0'] + "百" + digit[ch[2]-'0']+"拾";
  98.                                 }
  99.                                 break;
  100.                         default:
  101.                                 numStr = numStr + digit[ch[0]-'0'] + "仟" +digit[ch[1]-'0'] + "百" + digit[ch[2]-'0']+"拾"+digit[ch[3]-'0'];
  102.                 }
  103.                
  104.                
  105. //                System.out.println(numStr);
  106.                 return numStr;

  107.         }
  108. }
复制代码



作者: fmi110    时间: 2015-9-5 12:34
运行结果:
输入数学数字:
1
壹圆整

输入数学数字:
12
壹拾貳圆整
输入数学数字:
1003
壹仟零叁圆整

输入数学数字:
101000001010
壹仟零壹拾亿零壹仟零壹拾圆整




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2