1、工具
public class RmbUtils {
private RmbUtils() {
}
/**
* 汉语中数字大写
*/
private static final String[] CN_UPPER_NUMBER = { "零", "壹", "贰", "叁", "肆",
"伍", "陆", "柒", "捌", "玖" };
/**
* 汉语中货币单位大写,这样的设计类似于占位符
*/
private static final String[] CN_UPPER_MONETRAY_UNIT = {
"分", "角", "圆",
"拾", "佰", "仟", "万",
"拾", "佰", "仟", "亿",
"拾", "佰", "仟", "兆",
"拾", "佰", "仟", "京",
"拾", "佰", "仟", "垓",
"拾", "佰", "仟", "杼",
"拾", "佰", "仟", "穰",
"拾", "佰", "仟", "沟",
"拾", "佰", "仟", "涧",
"拾", "佰", "仟", "正",
"拾", "佰", "仟", "载",
"拾", "佰", "仟", "极",
"拾", "佰", "仟" };
/**
* 特殊字符:整
*/
private static final String CN_FULL = "整";
/**
* 特殊字符:负
*/
private static final String CN_NEGATIVE = "负";
/**
* 金额的精度,默认值为2
*/
private static final int MONEY_PRECISION = 2;
/**
* 特殊字符:零元整
*/
private static final String CN_ZEOR_FULL = "零元" + CN_FULL;
private static final String REGEX = "-?([1-9][0-9]{0,51})|0|([0]\\.[0-9]{1,2})|([1-9][0-9]{0,51}\\.[0-9]{1,2})";
public static String rmbUpperWithZero(String numberOfMoney)throws Exception {
return rmbUpper(numberOfMoney, true);
}
public static String rmbUpperWithoutZero(String numberOfMoney) throws Exception {
return rmbUpper(numberOfMoney, false);
}
public static String rmbUpper(String numberOfMoney, boolean withZero) throws Exception {
if(!numberOfMoney.matches(REGEX)){
throw new Exception("金额格式错误!");
}
return rmbUpperBigDecimal(new BigDecimal(numberOfMoney), withZero);
}
/**
* 把输入的金额转换为汉语中人民币的大写
*
* @param numberOfMoney
* 输入的金额
* withZero 505000 是否带0,true:50万零五千,false:则是50万五千
* @return 对应的汉语大写
*/
private static String rmbUpperBigDecimal(BigDecimal numberOfMoney, boolean withZero) {
StringBuffer sb = new StringBuffer();
// -1, 0, or 1 as the value of this BigDecimal is negative, zero, or
// positive.
int signum = numberOfMoney.signum();
// 零元整的情况
if (signum == 0) {
return CN_ZEOR_FULL;
}
//这里会进行金额的四舍五入
BigDecimal number = numberOfMoney.movePointRight(MONEY_PRECISION).setScale(0, RoundingMode.DOWN).abs();
// 得到小数点后两位值
long scale = number.remainder(new BigDecimal(100)).longValue();
int numUnit = 0;
int numIndex = 0;
boolean getZero = false;
// 判断最后两位数,一共有四中情况:00 = 0, 01 = 1, 10, 11
if (!(scale > 0)) {
numIndex = 2;
number = number.divide(new BigDecimal(100), 0, RoundingMode.DOWN);
getZero = true;
}
if (scale > 0 && (scale % 10 == 0)) {
numIndex = 1;
number = number.divide(new BigDecimal(10), 0, RoundingMode.DOWN);
getZero = false;
}
int zeroSize = 0;
int beginUnit = 0; //跨单位计算
while (true) {
if (number.compareTo(new BigDecimal(0)) == 0) {
break;
}
// 每次获取到最后一个数
numUnit = number.remainder(new BigDecimal(10)).intValue();
if (numUnit > 0) {
if ( beginUnit >= 4 && numIndex - beginUnit != 2 &&!withZero) {
sb.insert(0, CN_UPPER_NUMBER[0]);
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex - ((numIndex - 2) % 4)]);
}
if ((numIndex == 9) && (zeroSize >= 3)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
}
if ((numIndex == 13) && (zeroSize >= 3)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
}
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
sb.insert(0, CN_UPPER_NUMBER[numUnit]);
getZero = false;
zeroSize = 0;
beginUnit = 0;
} else {
++zeroSize;
boolean isAdd = false;
if (beginUnit > 0 && !withZero) {
isAdd = true;
beginUnit += 1;
}
if (!(getZero)) {
if ((numIndex - 2) % 4 == 0 && !withZero) {
if (!isAdd) {
beginUnit += 1;
}
} else {
sb.insert(0, CN_UPPER_NUMBER[numUnit]);
}
}
if (numIndex == 2) {
if (number.compareTo(new BigDecimal(0)) > 0) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
}
} else if (((numIndex - 2) % 4 == 0) && number.remainder(new BigDecimal(1000)).intValue() > 0) {
if (beginUnit < 4) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
}
}
getZero = true;
}
// 让number每次都去掉最后一个数
number = number.divide(new BigDecimal(10), 0, RoundingMode.DOWN);
++numIndex;
}
// 如果signum == -1,则说明输入的数字为负数,就在最前面追加特殊字符:负
if (signum == -1) {
sb.insert(0, CN_NEGATIVE);
}
// 输入的数字小数点后两位为"00"的情况,则要在最后追加特殊字符:整
if (!(scale > 0)) {
sb.append(CN_FULL);
}
return sb.toString();
}
/**
* 把输入的金额转换为汉语中人民币的大写
*
* @param numberOfMoney
* 输入的金额
* withZero 505000 是否带0,true:50万零五千,false:则是50万五千
* @return 对应的汉语大写
*/
private static String rmbUpperLong(BigDecimal numberOfMoney, boolean withZero) {
StringBuffer sb = new StringBuffer();
// -1, 0, or 1 as the value of this BigDecimal is negative, zero, or
// positive.
int signum = numberOfMoney.signum();
// 零元整的情况
if (signum == 0) {
return CN_ZEOR_FULL;
}
//这里会进行金额的四舍五入
long number = numberOfMoney.movePointRight(MONEY_PRECISION)
.setScale(0, 4).abs().longValue();
// 得到小数点后两位值
long scale = number % 100;
int numUnit = 0;
int numIndex = 0;
boolean getZero = false;
// 判断最后两位数,一共有四中情况:00 = 0, 01 = 1, 10, 11
if (!(scale > 0)) {
numIndex = 2;
number = number / 100;
getZero = true;
}
if (scale > 0 && (scale % 10 == 0)) {
numIndex = 1;
number = number / 10;
getZero = false;
}
int zeroSize = 0;
int beginUnit = 0; //跨单位计算
while (true) {
if (number <= 0) {
break;
}
// 每次获取到最后一个数
numUnit = (int) (number % 10);
if (numUnit > 0) {
if (beginUnit >= 4 && !withZero) {
sb.insert(0, CN_UPPER_NUMBER[0]);
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex - ((numIndex - 2) % 4)]);
}
if ((numIndex == 9) && (zeroSize >= 3)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
}
if ((numIndex == 13) && (zeroSize >= 3)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
}
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
sb.insert(0, CN_UPPER_NUMBER[numUnit]);
getZero = false;
zeroSize = 0;
beginUnit = 0;
} else {
++zeroSize;
if (beginUnit > 0 && !withZero) {
beginUnit += 1;
}
if (!(getZero)) {
if ((numIndex - 2) % 4 == 0 && !withZero) {
beginUnit += 1;
} else {
sb.insert(0, CN_UPPER_NUMBER[numUnit]);
}
}
if (numIndex == 2) {
if (number > 0) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
}
} else if (((numIndex - 2) % 4 == 0) && number % 1000 > 0) {
if (beginUnit < 4) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
}
}
getZero = true;
}
// 让number每次都去掉最后一个数
number = number / 10;
++numIndex;
}
// 如果signum == -1,则说明输入的数字为负数,就在最前面追加特殊字符:负
if (signum == -1) {
sb.insert(0, CN_NEGATIVE);
}
// 输入的数字小数点后两位为"00"的情况,则要在最后追加特殊字符:整
if (!(scale > 0)) {
sb.append(CN_FULL);
}
return sb.toString();
}
}