/**
* 人民币小写转大写类
* 允许最大值(9999,9999,9999,9999.99)
* 未解决问题:如果以double变量为参数,大于100万亿,小数部分会有误差
*
*/
import java.math.BigDecimal;
class RMB implements Comparable {
private String rmb_n, rmb_c;
private BigDecimal rmb;
private static final String[] daxie = { "零", "壹", "贰", "叁", "肆", "伍", "陆",
"柒", "捌", "玖" };
private static final String[] danwei = { "", "万", "亿", "万" };
public RMB(double r) {
BigDecimal rmb = new BigDecimal(r + "");
this.rmb = rmb;
this.rmb_n = format(this.rmb);
this.rmb_c = ntoc(this.rmb_n);
}
public RMB(String rmb_n) {
rmb_n = rmb_n.replaceFirst("rmb", "");
rmb_n = rmb_n.replaceFirst("RMB", "");
this.rmb = new BigDecimal(rmb_n);
this.rmb_n = format(this.rmb);
this.rmb_c = ntoc(this.rmb_n);
}
public String toString() {
int index = rmb_n.length() - 3;
String ss = rmb_n.substring(index);
while (index - 4 > 0) {
index -= 4;
ss = "," + rmb_n.substring(index, index + 4) + ss;
}
ss = rmb_n.substring(0, index) + ss;
return "小写: " + ss + "/n大写: " + rmb_c;
}
public int compareTo(Object o) {
BigDecimal bd = (BigDecimal) o;
return this.rmb.compareTo(bd);
}
public static String format(BigDecimal rmb) {
String s = rmb.multiply(new BigDecimal(100.0)).longValue() + "";
while (s.length() < 3) {
s = "0" + s;
}
return s.substring(0, s.length() - 2) + "."
+ s.substring(s.length() - 2, s.length());
}
public static String toChinese(double d) {
BigDecimal bd = new BigDecimal(d + "");
return ntoc(format(bd));
}
public static String toChinese(String s) {
s = s.replaceFirst("rmb", "");
s = s.replaceFirst("RMB", "");
BigDecimal bd = new BigDecimal(s);
return ntoc(format(bd));
}
private static String ntoc(String s) {
String ss = "", ss_add = "";
if (s.charAt(0) != '0') {
int index = s.length() - 3, i = 0;
while (index > 0) {
if (i++ == 4)
return "错误:超过最大值";
index -= 4;
if (index >= 0)
ss_add = change4bit(s.substring(index, index + 4));
else
ss_add = change4bit(s.substring(0, index + 4));
ss_add += danwei[i - 1];
ss = ss_add + ss;
}
ss = ss.replaceAll("亿万", "亿零");
ss = ss.replaceAll("零零", "零");
if ((ss.charAt(0) + "").equals(daxie[0]))
ss = ss.substring(1);
while ((ss.charAt(ss.length() - 1) + "").equals(daxie[0])) {
ss = ss.substring(0, ss.length() - 1);
}
ss += "圆";
}
ss_add = s.substring(s.length() - 2, s.length());
if (ss_add.equals("00"))
return ss + "整";
ss += ntoc_bit(ss_add.charAt(0));
if (ss_add.charAt(0) != '0')
ss += "角";
if (ss_add.charAt(1) != '0')
ss += ntoc_bit(ss_add.charAt(1)) + "分";
if ((ss.charAt(0) + "").equals(daxie[0]))
ss = ss.substring(1);
return ss;
}
private static String change4bit(String s) {
while (s.length() < 4) {
s = "0" + s;
}
String ss = "";
if (s.charAt(0) != '0')
ss += ntoc_bit(s.charAt(0)) + "仟";
if (s.charAt(1) != '0') {
if (s.charAt(0) == '0')
ss += daxie[0];
ss += ntoc_bit(s.charAt(1)) + "佰";
}
if (s.charAt(2) != '0') {
if (s.charAt(1) == '0')
ss += daxie[0];
ss += ntoc_bit(s.charAt(2)) + "拾";
}
if (s.charAt(3) != '0') {
if (s.charAt(2) == '0')
ss += daxie[0];
ss += ntoc_bit(s.charAt(3)) + "";
}
return ss;
}
private static String ntoc_bit(char c) {
return daxie[Integer.parseInt(c + "")];
}
}
public class runTestMain {
public static void main(String[] args)
{
RMB r1 = new RMB(0.99);
System.out.println(r1);
RMB r2 = new RMB("6543.21");
System.out.println(r2);
RMB r3 = new RMB("76543.21rmb");
System.out.println(r3);
RMB r4 = new RMB("19876543.21RMB");
System.out.println(r4);
RMB r5 = new RMB("rmb219876543.21");
System.out.println(r5);
RMB r6 = new RMB("RMB543219876543.21");
System.out.println(r6);
RMB r7 = new RMB("1234123412341234.99");
System.out.println(r7);
RMB r8 = new RMB("9999999999999999.99rmb");
System.out.println(r8);
RMB r9 = new RMB("10000000000000000.00");
System.out.println(r9);
System.out.println(RMB.toChinese(1));
System.out.println(RMB.toChinese(0.01));
System.out.println(RMB.toChinese(0.1));
System.out.println(RMB.toChinese(0.99));
System.out.println(RMB.toChinese(1.01));
System.out.println(RMB.toChinese(1.1));
System.out.println(RMB.toChinese(1.99));
System.out.println(RMB.toChinese(10000.1));
System.out.println(RMB.toChinese(100000000));
System.out.println(RMB.toChinese(100000000.01));
System.out.println(RMB.toChinese(110010000.00));
System.out.println(RMB.toChinese(1010101.01));
System.out.println(RMB.toChinese(100100100.1));
System.out.println(RMB.toChinese(100010001000.99));
System.out.println(RMB.toChinese(100001000010000.99));
System.out.println(RMB.toChinese("100001000010000.99"));
System.out.println(RMB.toChinese("1088001.0"));
}
}
结果
小写: 0.99
大写: 玖角玖分
小写: 6543.21
大写: 陆仟伍佰肆拾叁圆贰角壹分
小写: 7,6543.21
大写: 柒万陆仟伍佰肆拾叁圆贰角壹分
小写: 1987,6543.21
大写: 壹仟玖佰捌拾柒万陆仟伍佰肆拾叁圆贰角壹分
小写: 2,1987,6543.21
大写: 贰亿壹仟玖佰捌拾柒万陆仟伍佰肆拾叁圆贰角壹分
小写: 5432,1987,6543.21
大写: 伍仟肆佰叁拾贰亿壹仟玖佰捌拾柒万陆仟伍佰肆拾叁圆贰角壹分
小写: 1234,1234,1234,1234.99
大写: 壹仟贰佰叁拾肆万壹仟贰佰叁拾肆亿壹仟贰佰叁拾肆万壹仟贰佰叁拾肆圆玖角玖分
小写: 9999,9999,9999,9999.99
大写: 玖仟玖佰玖拾玖万玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰玖拾玖圆玖角玖分
小写: 1,0000,0000,0000,0000.00
大写: 错误:超过最大值
壹圆整
壹分
壹角
玖角玖分
壹圆零壹分
壹圆壹角
壹圆玖角玖分
壹万圆壹角
壹亿圆整
壹亿圆零壹分
壹亿壹仟零壹万圆整
壹佰零壹万零壹佰零壹圆零壹分
壹亿零壹拾万零壹佰圆壹角
壹仟亿壹仟万壹仟圆玖角玖分
壹佰万零壹拾亿零壹万圆玖角捌分
壹佰万零壹拾亿零壹万圆玖角玖分
壹佰零捌万捌仟零壹圆整