廢話(huà)不多說(shuō)纽甘,直接貼代碼
// 金額大寫(xiě)轉(zhuǎn)化函數(shù) add by zhangbg 2018-11-12 start
//money 數(shù)字
//currencyunit 金額單位
function changeMoneyToChinese(money,currencyunit) {
if(currencyunit == "萬(wàn)元" || currencyunit == "萬(wàn)元/畝"){
// 修改萬(wàn)元金額精度默認(rèn)保留6位小數(shù)
money = (money * 10000).toFixed(6);
// end
}
var cnNums = new Array("零", "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖"); // 漢字的數(shù)字
var cnIntRadice = new Array("", "拾", "佰", "仟"); // 基本單位
var cnIntUnits = new Array("", "萬(wàn)", "億", "兆"); // 對(duì)應(yīng)整數(shù)部分?jǐn)U展單位
var cnDecUnits = new Array("角", "分", "毫", "厘"); // 對(duì)應(yīng)小數(shù)部分單位
var cnInteger = "整"; // 整數(shù)金額時(shí)后面跟的字符
var cnIntLast = "元"; // 整型完以后的單位
var IntegerNum; // 金額整數(shù)部分
var DecimalNum; // 金額小數(shù)部分
// 新增金額小數(shù)部分字符串
var DecimalStr = ""; // 金額小數(shù)部分字符串
// end
var ChineseStr = ""; // 輸出的中文金額字符串
var parts; // 分離金額后用的數(shù)組,預(yù)定義
if (money == "") {
return "";
}
money = parseFloat(money);
if (money == 0) {
ChineseStr = cnNums[0] + cnIntLast + cnInteger;
// ChineseStr = cnNums[0] + cnIntLast
//單位是萬(wàn)元每畝時(shí)在末尾添加每畝
if(currencyunit == "萬(wàn)元/畝"){
ChineseStr = ChineseStr + "每畝";
}
return ChineseStr;
}
money = money.toString(); // 轉(zhuǎn)換為字符串
if (money.indexOf(".") == -1) {
IntegerNum = money;
DecimalNum = '';
} else {
parts = money.split(".");
IntegerNum = parts[0];
DecimalNum = parts[1].substr(0, 4);
}
if (parseInt(IntegerNum, 10) > 0) {// 獲取整型部分轉(zhuǎn)換
zeroCount = 0;
IntLen = IntegerNum.length;
for (i = 0; i < IntLen; i++) {
n = IntegerNum.substr(i, 1);
p = IntLen - i - 1;
q = p / 4;
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 != '') {// 小數(shù)部分
// [start]
decLen = DecimalNum.length;
for (i = 0; i < decLen; i++) {
n = DecimalNum.substr(i, 1);
if (n != '0') {
DecimalStr += cnNums[Number(n)] + cnDecUnits[i];
}
}
}
// 無(wú)小數(shù)部分時(shí)顯示xx元整
if (DecimalStr == '') {
ChineseStr += cnInteger;
} else {
ChineseStr += DecimalStr;
}
// end
if (ChineseStr == '') {
// 空字符串顯示零元整
ChineseStr += cnNums[0] + cnIntLast + cnInteger;
// end
}
//單位是萬(wàn)元每畝時(shí)在末尾添加每畝
if(currencyunit == "萬(wàn)元/畝"){
ChineseStr = ChineseStr + "每畝";
}
return ChineseStr;
}