/**
* 浮点数转换为大写货币金额
*
* @author Bobby
*
*/
public class ConvertFloatNumToRMBFormat {
private static String[] upperCaseArray = { "零", "壹", "贰", "叁", "肆", "伍",
"陆", "柒", "捌", "玖" };
private static String[] moneyUnitArray = { "元", "拾", "佰", "仟", "万", "拾",
"佰", "仟", "亿", "拾", "佰", "仟", "万" };
private static String[] decimalArray = { "角", "分" };
/**
* 把数字分成整数部分和小数部分
*
* @param num
* @return String[]
*/
private String[] getDividedStringNum(String num) {
int index = 0;
for (int i = 0; i < num.length(); i++) {
if (num.charAt(i) == '.')
break;
index++;
}
// 金额整数部分
String integerString = num.substring(0, index);
// 金额小数部分(精确到金额分)
String decialString = "0";
if (index < num.length() - 1)
decialString = num.substring(index + 1);
return new String[] { integerString, decialString };
}
private String[] getDividedStringNum(double num) {
// 金额整数部分
long integerData = (long) num;
// 金额小数部分(精确到金额分)
long decialData = Math.round((num - integerData) * 100);// 四舍五入
String decialString = String.valueOf(decialData);
// 如果金额角为0
if (decialData < 10) {
// 例如:0->00
decialString = "0" + decialData;
}
// 返回金额整数部分和小数部分
return new String[] { String.valueOf(integerData), decialString };
}
/**
* 转换金额的整数部分
*
* @param integerStr
* @return String
*/
private String getIntegerStr(String integerStr) {
String result = "";
boolean isZero = true;
int numLength = integerStr.length();
if (numLength > 13) {
System.out.println("超出本程序的金额范围");
} else {
for (int i = 0; i < numLength; i++) {
// 获取i位置的数字
int num = integerStr.charAt(i) - 48;
if (num != 0) {
isZero = false;
result += upperCaseArray[num]
+ moneyUnitArray[numLength - i - 1];
} else {
// 如果有多个连续的0只显示一个零,比如10001转为壹万零壹,不是壹万零零零壹
if (i > 0
&& !result.substring(result.length() - 1).equals(
"零")) {
result += upperCaseArray[num];
}
// 如果之前有不为0的数字,并且到了计数单位点
if (!isZero && (numLength - i - 1) % 4 == 0) {
// 标志重新记为0
isZero = true;
// 如果计数单位点前有零,去掉零
if (result.substring(result.length() - 1).equals("零")) {
result = result.substring(0, result.length() - 1);
}
// 添加计数单位
result += moneyUnitArray[(numLength - i - 1) / 4 * 4];
}
// 如果最后一个字符是零替换为元
if (i == numLength - 1
&& result.substring(result.length() - 1)
.equals("零")) {
result = result.substring(0, result.length() - 1) + "元";
}
}
}
}
return result;
}
private String getDecimalStr(String decimalStr) {
String result = "";
if (null != decimalStr && !"".equals(decimalStr)) {
int allNum = Integer.parseInt(decimalStr);
if (allNum > 0) {
int numLength = decimalStr.length();
for (int i = 0; i < numLength; i++) {
int num = decimalStr.charAt(i) - 48;
if (num != 0) {
result += upperCaseArray[num] + decimalArray[i];
} else {
if (i < 1) {
result += upperCaseArray[num];
}
}
}
} else {
result = "整";
}
}
return result;
}
private String getConvertedString(double moneyData) {
String[] dividedStringNumArray = getDividedStringNum(moneyData);
String integerStr = dividedStringNumArray[0];
String result = getIntegerStr(integerStr);
if (null != result && !"".equals(result)) {
String decimalStr = dividedStringNumArray[1];
result += getDecimalStr(decimalStr);
}
return result;
}
private String getConvertedString(String moneyString) {
String[] dividedStringNumArray = getDividedStringNum(moneyString);
String integerStr = dividedStringNumArray[0];
String result = getIntegerStr(integerStr);
if (null != result && !"".equals(result)) {
String decimalStr = dividedStringNumArray[1];
result += getDecimalStr(decimalStr);
}
return result;
}
public static void main(String[] args) {
ConvertFloatNumToRMBFormat nr = new ConvertFloatNumToRMBFormat();
double moneyData = 10000000001.01;
String moneyString = "10000000001";
System.out.println(nr.getConvertedString(moneyData));
System.out.println(nr.getConvertedString(moneyString));
}
<pre>}
输出结果:
壹佰亿零壹元零壹分
壹佰亿零壹元整