JAVA如何实现人民币大小写转换

本程序的作用是将人民币的小写形式转换为大写,例如:调用upper(“18.5″),将会返回字符串“壹拾捌圆伍角”。

[java] view plaincopy

  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;i0].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
  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. }

你可能感兴趣的:(JAVA如何实现人民币大小写转换)