function numberToChinese(num) {
var cnNums = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"];
var cnIntRadice = ["","拾","佰","仟"];
var cnIntUnits = ["","万","亿","兆"];
var cnDecUnits = ["角","分","毫","厘"];
var cnInteger = "整";
var cnIntLast = "元";
var maxNum = 999999999999999.9999;
var integerNum, decimalNum, chineseStr = "";
var parts;
if (num === "") {
return "";
}
num = parseFloat(num);
if (num >= maxNum) {
return "";
}
if (isNaN(num)) {
return "";
}
if (num === 0) {
chineseStr = cnNums[0] + cnIntLast + cnInteger;
return chineseStr;
}
num = num.toString();
if (num.indexOf(".") === -1) {
integerNum = num;
decimalNum = '';
} else {
parts = num.split(".");
integerNum = parts[0];
decimalNum = parts[1].substr(0, 4);
}
if (parseInt(integerNum, 10) > 0) {
var zeroCount = 0;
var IntLen = integerNum.length;
for (var i = 0; i < IntLen; i++) {
var n = integerNum.substr(i, 1);
var p = IntLen - i - 1;
var q = p / 4;
var m = p % 4;
if (n === "0") {
zeroCount++;
} else {
if (zeroCount > 0) {
chineseStr += cnNums[0];
}
zeroCount = 0;
chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
}
if (m === 0 && zeroCount < 4) {
chineseStr += cnIntUnits[q];
}
}
chineseStr += cnIntLast;
}
if (decimalNum !== '') {
var decLen = decimalNum.length;
for (var j = 0; j < decLen; j++) {
var m = decimalNum.substr(j, 1);
if (m !== '0') {
chineseStr += cnNums[Number(m)] + cnDecUnits[j];
}
}
}
if (chineseStr === '') {
chineseStr += cnNums[0] + cnIntLast + cnInteger;
} else if (decimalNum === '') {
chineseStr += cnInteger;
}
// 处理 "一十" 的情况
chineseStr = chineseStr.replace("壹拾", "拾");
// 处理 "零拾" 的情况
chineseStr = chineseStr.replace("零拾", "零");
// 去除多余的 "零"
while (chineseStr.indexOf("零零") !== -1) {
chineseStr = chineseStr.replace("零零", "零");
}
// 去除连续的 "零",保留一个
chineseStr = chineseStr.replace(/(零)\1+/g, "零");
// 去除结尾的 "零"
chineseStr = chineseStr.replace(/(零)+$/, "");
return chineseStr;
}
传入 89.56 得到 捌拾玖元伍角陆分
传入 1001 得到 壹仟零壹元整
传入 1001.2356 得到 壹仟零壹元贰角叁分伍毫陆厘