JAVA如何實現人民幣大小寫轉換
阿新 • • 發佈:2019-01-09
- package com.zzb.test0;
- import junit.framework.Assert;
- import org.junit.Test;
- /**
- * 測試人民幣小寫轉大寫
- * @author zhengzhb
- */
- public class UpperMoney {
- public String upper(String money) throws Exception{
- if(!money.matches(“^[1-9]+[0-9]*$|^[1-9]+[0-9]*.[0-9]+$” )){
- throw new Exception(“錢數格式錯誤!”
- }
- String[] part= money.split(“\\.”);
- StringBuffer integer = new StringBuffer();
- for(int i=0;i<part[0].length();i++){
- char perchar = part[0].charAt(i);
- integer.append(this.upperNumber(perchar));
- integer.append(this.upperNumber(part[0
- }
- StringBuffer decimal = new StringBuffer();
- if(part.length>1 && !“00”.equals(part[1])){
- int length = part[1].length()>=2?2:part[1].length();
- for(int i=0;i<length;i++){
- char perchar = part[1].charAt(i);
- decimal.append(this
- if(i==0)decimal.append(‘角’);
- if(i==1)decimal.append(‘分’);
- }
- }
- String result = integer.toString()+decimal.toString();
- return this.dispose(result);
- }
- private char upperNumber(char number){
- switch(number){
- case ‘0’: return ‘零’;
- case ‘1’: return ‘壹’;
- case ‘2’: return ‘貳’;
- case ‘3’: return ‘叄’;
- case ‘4’: return ‘肆’;
- case ‘5’: return ‘伍’;
- case ‘6’: return ‘陸’;
- case ‘7’: return ‘柒’;
- case ‘8’: return ‘捌’;
- case ‘9’: return ‘玖’;
- }
- return 0;
- }
- private char upperNumber(int index){
- switch(index){
- case 0: return ‘圓’;
- case 1: return ‘拾’;
- case 2: return ‘佰’;
- case 3: return ‘仟’;
- case 4: return ‘萬’;
- case 5: return ‘拾’;
- case 6: return ‘佰’;
- case 7: return ‘仟’;
- case 8: return ‘億’;
- case 9: return ‘拾’;
- case 10: return ‘佰’;
- case 11: return ‘仟’;
- }
- return 0;
- }
- private String dispose(String result){
- result = result.replaceAll(“零仟零佰零拾|零仟零佰|零佰零拾|零仟|零佰|零拾”, “零”)
- .replaceAll(“零+”, “零”).replace(“零億”, “億”);
- result = result.matches(“^.*億零萬[^零]仟.*$” )?result.replace(“零萬”, “零”)
- :result.replace(“零萬”, “萬”);
- result = result.replace(“億萬”, “億”).replace(“零圓”, “圓”).replace(“零分”, “”)
- .replaceAll(“圓零角零分|圓零角$|圓$” , “圓整”);
- return result;
- }
- @Test
- public void test(){
- try {
- Assert.assertEquals(upper(“1”), “壹圓整”);
- Assert.assertEquals(upper(“10”), “壹拾圓整”);
- Assert.assertEquals(upper(“20.7”), “貳拾圓柒角”);
- Assert.assertEquals(upper(“10.70”), “壹拾圓柒角”);
- Assert.assertEquals(upper(“18.5”), “壹拾捌圓伍角”);
- Assert.assertEquals(upper(“200.5”), “貳佰圓伍角”);
- Assert.assertEquals(upper(“2000”), “貳仟圓整”);
- Assert.assertEquals(upper(“50000”), “伍萬圓整”);
- Assert.assertEquals(upper(“500000”), “伍拾萬圓整”);
- Assert.assertEquals(upper(“5000000”), “伍佰萬圓整”);
- Assert.assertEquals(upper(“50000000”), “伍仟萬圓整”);
- Assert.assertEquals(upper(“500000000”), “伍億圓整”);
- Assert.assertEquals(upper(“5000000000”), “伍拾億圓整”);
- Assert.assertEquals(upper(“5000000001”), “伍拾億零壹圓整”);
- Assert.assertEquals(upper(“5000000021”), “伍拾億零貳拾壹圓整”);
- Assert.assertEquals(upper(“5000000421”), “伍拾億零肆佰貳拾壹圓整”);
- Assert.assertEquals(upper(“5000005421”), “伍拾億零伍仟肆佰貳拾壹圓整”);
- Assert.assertEquals(upper(“5000035421”), “伍拾億零叄萬伍仟肆佰貳拾壹圓整”);
- Assert.assertEquals(upper(“5000635421”), “伍拾億零陸拾叄萬伍仟肆佰貳拾壹圓整”);
- Assert.assertEquals(upper(“5004635421”), “伍拾億零肆佰陸拾叄萬伍仟肆佰貳拾壹圓整”);
- Assert.assertEquals(upper(“5024635421”), “伍拾億貳仟肆佰陸拾叄萬伍仟肆佰貳拾壹圓整”);
- Assert.assertEquals(upper(“5224635421”), “伍拾貳億貳仟肆佰陸拾叄萬伍仟肆佰貳拾壹圓整”);
- Assert.assertEquals(upper(“15224635421”), “壹佰伍拾貳億貳仟肆佰陸拾叄萬伍仟肆佰貳拾壹圓整”);
- Assert.assertEquals(upper(“215224635421”), “貳仟壹佰伍拾貳億貳仟肆佰陸拾叄萬伍仟肆佰貳拾壹圓整”);
- Assert.assertEquals(upper(“500021”), “伍拾萬零貳拾壹圓整”);
- Assert.assertEquals(upper(“5000821”), “伍佰萬零捌佰貳拾壹圓整”);
- Assert.assertEquals(upper(“5050006501”), “伍拾億伍仟萬陸仟伍佰零壹圓整”);
- Assert.assertEquals(upper(“550300001”), “伍億伍仟零叄拾萬零壹圓整”);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }