首页 > 技术文章 > java递归算法实现 数字人民币大写转换

jjcc 2017-02-28 18:33 原文

最近穷死了 ,没钱吃饭ing 写点钱给自己吧!
public class Test{
public static String getChar(long a){
int b = (int)a;
Map map = new HashMap();
map.put(0,"零");
map.put(1,"壹");
map.put(2,"贰");
map.put(3,"叁");
map.put(4,"肆");
map.put(5,"伍");
map.put(6,"陆");
map.put(7,"柒");
map.put(8,"捌");
map.put(9,"玖");
return map.get(b).toString();
}


public static void main(String args[]) throws Exception {
      String chinese0 = getChineseMoney(1007L);
System.out.println("我有"+chinese0 + "圓");

String chinese = getChineseMoney(10155530665347L);
System.out.println("我有"+chinese + "圓");

String chinese2 = getChineseMoney(10100065347L);
System.out.println("我有"+chinese2 +"圓");

String chinese3 = getChineseMoney(1013333223300065347L);
System.out.println("我穷死了。"+chinese3 +"圓");
    }

public static String getChineseMoney(long alb){
int len = String.valueOf(alb).length();
switch (len){
case 1:
return getChar(alb);
case 2:
return alb % 10 == 0 ? (alb/10 == 1 ? "": getChar(alb/10))+"拾" : getChar(alb/10)+"拾" +getChar(alb%10);
case 3:
if( alb % 100 == 0) {
return getChar(alb / 100) + "佰";
}
if(String.valueOf(alb).charAt(1) == '0'){
return getChar(alb / 100) + "佰"+getChar(0)+getChar(alb % 100);
}else{
return getChar(alb / 100) + "佰" + getChineseMoney(alb % 100);
}
case 4:
if( alb % 1000 == 0) {
return getChar(alb / 1000) + "仟";
}
if(String.valueOf(alb).charAt(1) == '0' && String.valueOf(alb).charAt(2) == '0'){
return getChar(alb / 1000) + "仟"+getChar(0)+getChar(alb % 100);
}else if(String.valueOf(alb).charAt(1) == '0'){
return getChar(alb / 1000) + "仟" + getChar(0) + getChineseMoney(alb % 100);
} else {
return getChar(alb / 1000) + "仟" + getChineseMoney(alb % 1000);
}
case 5:

case 6:

case 7:

case 8:
return getChineseMoney(alb / 10000) + "万" + getChineseMoney(alb % 10000);
case 9:

case 10:

case 11:

case 12:
return getChineseMoney(alb/(10000*10000L)) + "亿" + getChineseMoney(alb % (10000*10000L));
case 13:

case 14:

case 15:
return getChineseMoney(alb/(10000*10000*10000L)) + "万" + getChineseMoney(alb % (10000*10000*10000L));
default:
return "我穷的只剩下钱了,自己无聊的话加上去吧!";

}
}
}

推荐阅读