package com.itheima;
public class Text10 {
/**
* 10、 金额转换,阿拉伯数字转换成中国传统形式。
*
* 例如:101000001010 转换为 壹仟零壹拾亿零壹仟零壹拾圆整
*
* @param args
*/
public static void main(String[] args) {
Text10 c = new Text10();
System.out.println(c.change("190348.78"));
}
public String change(String m) {
StringBuffer re = new StringBuffer("");
int i = 0;
int j = 0;
i = m.lastIndexOf(".");
if (i == -1)
i = m.length();
String b = m.substring(0, i);
char[] c = b.toCharArray();
StringBuffer s = null;
for (i = c.length - 1, j = 0; i >= 0; i--, j++) {
s = new StringBuffer("");
if (i != c.length - 1 && c[i + 1] != '0')
s.append(changeNum(c));
if (c != '0') {
switch (j % 4) {
case 1:
s.append("拾");
break;
case 2:
s.append("佰");
break;
case 3:
s.append("仟");
break;
default:
break;
}
}
if (j != 0 && j % 8 == 0)
s.append("亿");
else if (j != 0 && j % 4 == 0) {
s.append("万");
}
re = s.append(re);
}
re.append("圆");
i = m.lastIndexOf(".");
String d = null;
if (i != -1) {
d = m.substring(i + 1, m.length());
char[] e = d.toCharArray();
if (e.length == 1)
if (e[0] != '0')
re.append("零").append(changeNum(e[0])).append("角")
.toString();
if (e.length == 2)
if (e[0] != '0' || e[1] != '0')
re.append("零");
if (e[0] != '0')
re.append(changeNum(e[0])).append("角").toString();
if (e[1] != '0')
re.append(changeNum(e[1])).append("分").toString();
}
return re.toString();
}
public String changeNum(char c) {
String[] n = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
return n[c - '0'];
}
} |
|