JS之金额转换为大写
金额转换为大写函数如下:
export function convertToChineseCapital(n) { if (n == 0) { return "零"; } if (!/^(\+|-)?(0|[1-9]\d*)(\.\d+)?$/.test(n)) return "数据非法"; var unit = "仟佰拾亿仟佰拾万仟佰拾元角分", str = ""; n += "00"; var a = parseFloat(n); if (a < 0) { n = n.substr(1); } var p = n.indexOf('.'); if (p >= 0) { n = n.substring(0, p) + n.substr(p + 1, 2); } unit = unit.substr(unit.length - n.length); for (var i = 0; i < n.length; i++) str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i); if (a > 0) { return str.replace(/零(仟|佰|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整"); } else { return "负" + str.replace(/零(仟|佰|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整"); } }
2026-06-09优化(增加毫厘):
export function convertCurrency(money) { const cnNums = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"]; const cnIntRadice = ["", "拾", "佰", "仟"]; const cnIntUnits = ["", "万", "亿", "兆"]; const cnDecUnits = ["角", "分", "毫", "厘"]; const cnInteger = "整"; const cnIntLast = "元"; let maxNum = 999999999999999.9999; let integerNum, decimalNum, chineseStr = "", parts, isNegative = false; if (money === "") return ""; money = parseFloat(money); if (money < 0) { isNegative = true; money = Math.abs(money); } if (money >= maxNum) return ""; if (money === 0) { chineseStr = cnNums[0] + cnIntLast + cnInteger; return isNegative ? "负" + chineseStr : chineseStr; } money = money.toString(); 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) { var zeroCount = 0; var IntLen = integerNum.length; for (let i = 0; i < IntLen; i++) { let n = integerNum.substr(i, 1); let p = IntLen - i - 1; let q = p / 4; let 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 !== "") { let decLen = decimalNum.length; for (let i = 0; i < decLen; i++) { let n = decimalNum.substr(i, 1); if (n !== "0") chineseStr += cnNums[Number(n)] + cnDecUnits[i]; } } if (chineseStr === "") { chineseStr += cnNums[0] + cnIntLast + cnInteger; } else if (decimalNum === "") { chineseStr += cnInteger; } return isNegative ? "负" + chineseStr : chineseStr; }

浙公网安备 33010602011771号