数字转中文大写
package amountInWords;
public class AmountInWords {
private char[] num = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' };
private char[] unit = { '圆', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾',
'佰', '仟', '万' };
private char isInteger = '整';
public static void main(String[] args) {
AmountInWords m = new AmountInWords();
char[] l = m.count(2125000823465.45).toCharArray();
for (int i = 0; i < l.length; i++) {
System.out.print(l[l.length - i - 1]);
}
}
public String count(Double i) {
Double d = ((double) (Math.round(((i - i.longValue()) * 1000) / 10)) / 100);// 保留两位小数 会出现精度丢失的问题 建议用BigDecimal// 取小数部分
char[] douPart = d.toString().substring(2).toCharArray();
char[] m = ((Long) i.longValue()).toString().toCharArray(); // 强制转换Long取小数部分
// 使之不会用科学计算表示
StringBuffer sbf = new StringBuffer("");
char[] intPart = new char[m.length];
if (d != 0) {
for (int j2 = 0; j2 < num.length; j2++) {
if (Character.getNumericValue(douPart[1]) == j2) {
sbf.append("分").append(num[j2]);
}
}
for (int j2 = 0; j2 < num.length; j2++) {
if (Character.getNumericValue(douPart[0]) == j2) {
sbf.append("角").append(num[j2]);
}
}
} else {
sbf.append(isInteger);
}
for (int j = 0; j < m.length; j++) {
for (int j2 = 0; j2 < num.length; j2++) {
if (Character.getNumericValue(m[j]) == j2) { // 比较其unicode码
intPart[m.length - j - 1] = num[j2];
}
}
}
for (int j = 0; j < intPart.length; j++) {
if (intPart[j] == '零') {
if (unit[j] == '亿') {
sbf.append(unit[j]);
} else if (unit[j] == '万') {
sbf.append(unit[j]);
}
continue;
}
sbf.append(unit[j]).append(intPart[j]);
}
return sbf.toString();
}
}