金额小写到大写的转变

package com.liba.link;

public class Exchange {

private static final String chs[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾" };

/**
*
* @param money
* 金额数字串
* @return 金额中文大写 将输入数字串money分割成4位数,将四位数转换成金额大写后再加上单位("","万","亿")
*/
public String numToRMB(String money) {
StringBuilder chRMB = new StringBuilder();

if(money.equals("0.00")){
return "零元";
}

try {
double t = Double.parseDouble(money);
if (t < 0) {
System.out.println("金额不能是负的!");
throw new IllegalArgumentException("金额不能是负的!");
}
} catch (NumberFormatException e) {
System.out.println("输入数据有误,请检查!");
throw new IllegalArgumentException("输入数据有误,请检查!");
}

int dotPos = money.indexOf('.');
String strz;// 截取整数部分
if (dotPos == -1) {
strz = money;
} else {
strz = money.substring(0, dotPos);// 整数部分
}

StringBuilder sb = new StringBuilder(strz);
strz = sb.toString();
String cur = "";
boolean zero = false;// 记录是否应该加‘零’如 2001,读作 贰仟零壹元
String temp;
String digits;// 每次截取的最后四位数
while (strz.length() > 0) {

if (strz.length() >= 4) {
digits = strz.substring(strz.length() - 4);
strz = strz.substring(0, strz.length() - 4);
} else {
digits = strz;
strz = "";

}
temp = fourMaxChange(digits);
if (temp.equals("零")) {
if (cur.equals("亿")) {
chRMB.insert(0, "亿");
}
if (zero == true) {
if (!chRMB.substring(0, 1).equals("零"))
chRMB.insert(0, "零");
zero = false;
}
} else {
temp += cur;
chRMB.insert(0, temp);
if (digits.charAt(0) == '0')
if (chRMB.length() >= 1
&& !chRMB.substring(0, 1).equals("零"))
chRMB.insert(0, "零");
zero = true;

}
if (cur.equals("")) {// 改变单位
cur = "万";
} else if (cur.equals("亿")) {
cur = "万";
} else
cur = "亿";

}
String strx;
if (dotPos != -1) {// 有小数
strx = money.substring(dotPos + 1);
if (strx.length() > 2) {
System.out.println("金额只允许两位小数!");
throw new IllegalArgumentException("金额只允许两位小数!");
// return null;
}
int xiaoshu = Integer.parseInt(strx);// 小数部分
if (xiaoshu == 0 || strx.length() == 0) {
chRMB.append("元");// 整
} else
chRMB.append("元");
if (strx.length() == 1) {
chRMB.append(chs[xiaoshu]).append("分");
} else {
if (xiaoshu / 10 != 0) {
chRMB.append(chs[xiaoshu / 10]).append("角");
} else {
// chRMB.append("零");
}
if (xiaoshu % 10 != 0) {
chRMB.append(chs[xiaoshu % 10]).append("分");
}
}
} else {
chRMB.append("元");
}

return chRMB.toString();
}

/**
*
* @param number
* 输入数字串
* @return 转换后的大写金额
* @throws NumberFormatException
* 不合法的数字 将最多四位整数转换成大写金额
*/
public String fourMaxChange(String number) throws NumberFormatException {// 将最多四位整数转换成大写金额
StringBuilder rs = new StringBuilder();
int money;
money = Integer.parseInt(number);
boolean zero = false;
if (money / 1000 != 0) {
zero = true;
rs.append(chs[money / 1000]).append("仟");
money %= 1000;
}

if (money / 100 != 0) {
zero = true;
rs.append(chs[money / 100]).append("佰");
money %= 100;
} else if (zero == true) {
rs.append(chs[0]);// 零
money %= 100;
}

if (money / 10 != 0) {
zero = true;
rs.append(chs[money / 10]).append("拾");
money %= 10;
} else if (zero == true && !rs.substring(rs.length() - 1).equals("零")) {
rs.append(chs[0]);// 零
money %= 10;
}
if (money != 0) {
zero = true;
rs.append(chs[money]);
} else if (zero == true && rs.substring(rs.length() - 1).equals("零")) {
rs.delete(rs.length() - 1, rs.length());
}
if (zero == true)
return rs.toString();
else
return chs[0];// 零
}

}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页