1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > JS工具函数 数字转为中文数字和大写金额

JS工具函数 数字转为中文数字和大写金额

时间:2022-03-18 13:29:06

相关推荐

JS工具函数 数字转为中文数字和大写金额

1. 数字转为中文数字

/*** 数字转汉字* @param num* @returns {string}*/function numberToChinese(num) {const AA = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十']const BB = ['', '十', '百', '仟', '萬', '億', '点', '']let a = ('' + num).replace(/(^0*)/g, '').split('.')let k = 0let re = ''for (let i = a[0].length - 1; i >= 0; i--) {switch (k) {case 0:re = BB[7] + rebreakcase 4:if (!new RegExp('0{4}//d{' + (a[0].length - i - 1) + '}$').test(a[0])) {re = BB[4] + re}breakcase 8:re = BB[5] + reBB[7] = BB[5]k = 0break}if (k % 4 === 2 && a[0].charAt(i + 2) !== 0 && a[0].charAt(i + 1) === 0) {re = AA[0] + re}if (a[0].charAt(i) !== 0) {re = AA[a[0].charAt(i)] + BB[k % 4] + re}k++}if (a.length > 1) // 加上小数部分(如果有小数部分){re += BB[6]for (let i = 0; i < a[1].length; i++) {re += AA[a[1].charAt(i)]}}if (re === '一十') {re = '十'}if (re.match(/^一/) && re.length === 3) {re = re.replace("一", "");}return re;}

数字转中文数字示例

let numTem = 1545644;console.log(numberToChinese(numTem)); // 一百五十四萬五仟六百四十四

2. 数字转为中文大写金额

/*** 数字转大写中文数字* @param npm {String} 数字* @returns {string}*/function changeToChineseMoney(npm) {//判断如果传递进来的不是字符的话转换为字符if (typeof npm == 'number') {npm = String(npm)}npm = npm.replace(/,/g, '') //替换tomoney()中的“,”npm = npm.replace(/ /g, '') //替换tomoney()中的空格npm = npm.replace(/¥/g, '') //替换掉可能出现的¥字符if (isNaN(npm)) { //验证输入的字符是否为数字//alert("请检查小写金额是否正确");return ''}//字符处理完毕后开始转换,采用前后两部分分别转换const part = String(npm).split('.')let newChar = ''//小数点前进行转化for (let i = part[0].length - 1; i >= 0; i--) {if (part[0].length > 10) {return ''//若数量超过拾亿单位,提示}let tmpNewChar = ''let perChar = part[0].charAt(i)switch (perChar) {case '0':tmpNewChar = '零' + tmpNewCharbreakcase '1':tmpNewChar = '壹' + tmpNewCharbreakcase '2':tmpNewChar = '贰' + tmpNewCharbreakcase '3':tmpNewChar = '叁' + tmpNewCharbreakcase '4':tmpNewChar = '肆' + tmpNewCharbreakcase '5':tmpNewChar = '伍' + tmpNewCharbreakcase '6':tmpNewChar = '陆' + tmpNewCharbreakcase '7':tmpNewChar = '柒' + tmpNewCharbreakcase '8':tmpNewChar = '捌' + tmpNewCharbreakcase '9':tmpNewChar = '玖' + tmpNewCharbreak}switch (part[0].length - i - 1) {case 0:tmpNewChar = tmpNewChar + '元'breakcase 1:if (perChar != 0) tmpNewChar = tmpNewChar + '拾'breakcase 2:if (perChar != 0) tmpNewChar = tmpNewChar + '佰'breakcase 3:if (perChar != 0) tmpNewChar = tmpNewChar + '仟'breakcase 4:tmpNewChar = tmpNewChar + '万'breakcase 5:if (perChar != 0) tmpNewChar = tmpNewChar + '拾'breakcase 6:if (perChar != 0) tmpNewChar = tmpNewChar + '佰'breakcase 7:if (perChar != 0) tmpNewChar = tmpNewChar + '仟'breakcase 8:tmpNewChar = tmpNewChar + '亿'breakcase 9:tmpNewChar = tmpNewChar + '拾'break}newChar = tmpNewChar + newChar}//小数点之后进行转化if (npm.indexOf('.') !== -1) {if (part[1].length > 2) {// alert("小数点之后只能保留两位,系统将自动截断");part[1] = part[1].substring(0, 2)}for (let i = 0; i < part[1].length; i++) {let tmpNewChar = ''let tmpPerChar = part[1].charAt(i)switch (tmpPerChar) {case '0':tmpNewChar = '零' + tmpNewCharbreakcase '1':tmpNewChar = '壹' + tmpNewCharbreakcase '2':tmpNewChar = '贰' + tmpNewCharbreakcase '3':tmpNewChar = '叁' + tmpNewCharbreakcase '4':tmpNewChar = '肆' + tmpNewCharbreakcase '5':tmpNewChar = '伍' + tmpNewCharbreakcase '6':tmpNewChar = '陆' + tmpNewCharbreakcase '7':tmpNewChar = '柒' + tmpNewCharbreakcase '8':tmpNewChar = '捌' + tmpNewCharbreakcase '9':tmpNewChar = '玖' + tmpNewCharbreak}if (i === 0) tmpNewChar = tmpNewChar + '角'if (i === 1) tmpNewChar = tmpNewChar + '分'newChar = newChar + tmpNewChar}}//替换所有无用汉字while (newChar.search('零零') !== -1) {newChar = newChar.replace('零零', '零')}newChar = newChar.replace('零亿', '亿')newChar = newChar.replace('亿万', '亿')newChar = newChar.replace('零万', '万')newChar = newChar.replace('零元', '元')newChar = newChar.replace('零角', '')newChar = newChar.replace('零分', '')if (newChar.charAt(newChar.length - 1) === '元') {newChar = newChar + '整'}return newChar}

数字转大写金额示例

let money = 15455605;console.log(changeToChineseMoney(money)); // 壹仟伍佰肆拾伍万伍仟陆佰零伍元整

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。