1. 程式人生 > >JAVA如何實現人民幣大小寫轉換

JAVA如何實現人民幣大小寫轉換

  1. package com.zzb.test0;
  2. import junit.framework.Assert;
  3. import org.junit.Test;
  4. /**
  5.  * 測試人民幣小寫轉大寫
  6.  * @author zhengzhb
  7.  */
  8. public class UpperMoney {
  9.     public String upper(String money) throws Exception{
  10.         if(!money.matches(“^[1-9]+[0-9]*$|^[1-9]+[0-9]*.[0-9]+$” )){
  11.             throw new Exception(“錢數格式錯誤!”
    );
  12.         }
  13.         String[] part= money.split(“\\.”);
  14.         StringBuffer integer = new StringBuffer();
  15.         for(int i=0;i<part[0].length();i++){
  16.             char perchar = part[0].charAt(i);
  17.             integer.append(this.upperNumber(perchar));
  18.             integer.append(this.upperNumber(part[0
    ].length()-i-1));
  19.         }
  20.         StringBuffer decimal = new StringBuffer();
  21.         if(part.length>1 && !“00”.equals(part[1])){
  22.             int length = part[1].length()>=2?2:part[1].length();
  23.             for(int i=0;i<length;i++){
  24.                 char perchar = part[1].charAt(i);
  25.                 decimal.append(this
    .upperNumber(perchar));
  26.                 if(i==0)decimal.append(‘角’);
  27.                 if(i==1)decimal.append(‘分’);
  28.             }
  29.         }
  30.         String result = integer.toString()+decimal.toString();
  31.         return this.dispose(result);
  32.     }
  33.     private char upperNumber(char number){
  34.         switch(number){
  35.             case ‘0’return ‘零’;
  36.             case ‘1’return ‘壹’;
  37.             case ‘2’return ‘貳’;
  38.             case ‘3’return ‘叄’;
  39.             case ‘4’return ‘肆’;
  40.             case ‘5’return ‘伍’;
  41.             case ‘6’return ‘陸’;
  42.             case ‘7’return ‘柒’;
  43.             case ‘8’return ‘捌’;
  44.             case ‘9’return ‘玖’;
  45.         }
  46.         return 0;
  47.     }
  48.     private char upperNumber(int index){
  49.         switch(index){
  50.             case 0return ‘圓’;
  51.             case 1return ‘拾’;
  52.             case 2return ‘佰’;
  53.             case 3return ‘仟’;
  54.             case 4return ‘萬’;
  55.             case 5return ‘拾’;
  56.             case 6return ‘佰’;
  57.             case 7return ‘仟’;
  58.             case 8return ‘億’;
  59.             case 9return ‘拾’;
  60.             case 10return ‘佰’;
  61.             case 11return ‘仟’;
  62.         }
  63.         return 0;
  64.     }
  65.     private String dispose(String result){
  66.         result = result.replaceAll(“零仟零佰零拾|零仟零佰|零佰零拾|零仟|零佰|零拾”“零”)
  67.             .replaceAll(“零+”“零”).replace(“零億”“億”);
  68.         result = result.matches(“^.*億零萬[^零]仟.*$” )?result.replace(“零萬”“零”)
  69.             :result.replace(“零萬”“萬”);
  70.         result = result.replace(“億萬”“億”).replace(“零圓”“圓”).replace(“零分”“”)
  71.             .replaceAll(“圓零角零分|圓零角$|圓$” , “圓整”);
  72.         return result;
  73.     }
  74.     @Test
  75.     public void test(){
  76.         try {
  77.             Assert.assertEquals(upper(“1”), “壹圓整”);
  78.             Assert.assertEquals(upper(“10”), “壹拾圓整”);
  79.             Assert.assertEquals(upper(“20.7”), “貳拾圓柒角”);
  80.             Assert.assertEquals(upper(“10.70”), “壹拾圓柒角”);
  81.             Assert.assertEquals(upper(“18.5”), “壹拾捌圓伍角”);
  82.             Assert.assertEquals(upper(“200.5”), “貳佰圓伍角”);
  83.             Assert.assertEquals(upper(“2000”), “貳仟圓整”);
  84.             Assert.assertEquals(upper(“50000”), “伍萬圓整”);
  85.             Assert.assertEquals(upper(“500000”), “伍拾萬圓整”);
  86.             Assert.assertEquals(upper(“5000000”), “伍佰萬圓整”);
  87.             Assert.assertEquals(upper(“50000000”), “伍仟萬圓整”);
  88.             Assert.assertEquals(upper(“500000000”), “伍億圓整”);
  89.             Assert.assertEquals(upper(“5000000000”), “伍拾億圓整”);
  90.             Assert.assertEquals(upper(“5000000001”), “伍拾億零壹圓整”);
  91.             Assert.assertEquals(upper(“5000000021”), “伍拾億零貳拾壹圓整”);
  92.             Assert.assertEquals(upper(“5000000421”), “伍拾億零肆佰貳拾壹圓整”);
  93.             Assert.assertEquals(upper(“5000005421”), “伍拾億零伍仟肆佰貳拾壹圓整”);
  94.             Assert.assertEquals(upper(“5000035421”), “伍拾億零叄萬伍仟肆佰貳拾壹圓整”);
  95.             Assert.assertEquals(upper(“5000635421”), “伍拾億零陸拾叄萬伍仟肆佰貳拾壹圓整”);
  96.             Assert.assertEquals(upper(“5004635421”), “伍拾億零肆佰陸拾叄萬伍仟肆佰貳拾壹圓整”);
  97.             Assert.assertEquals(upper(“5024635421”), “伍拾億貳仟肆佰陸拾叄萬伍仟肆佰貳拾壹圓整”);
  98.             Assert.assertEquals(upper(“5224635421”), “伍拾貳億貳仟肆佰陸拾叄萬伍仟肆佰貳拾壹圓整”);
  99.             Assert.assertEquals(upper(“15224635421”), “壹佰伍拾貳億貳仟肆佰陸拾叄萬伍仟肆佰貳拾壹圓整”);
  100.             Assert.assertEquals(upper(“215224635421”), “貳仟壹佰伍拾貳億貳仟肆佰陸拾叄萬伍仟肆佰貳拾壹圓整”);
  101.             Assert.assertEquals(upper(“500021”), “伍拾萬零貳拾壹圓整”);
  102.             Assert.assertEquals(upper(“5000821”), “伍佰萬零捌佰貳拾壹圓整”);
  103.             Assert.assertEquals(upper(“5050006501”), “伍拾億伍仟萬陸仟伍佰零壹圓整”);
  104.             Assert.assertEquals(upper(“550300001”), “伍億伍仟零叄拾萬零壹圓整”);
  105.         } catch (Exception e) {
  106.             e.printStackTrace();
  107.         }
  108.     }
  109. }