本文主要是介绍JS阿拉伯数字转为中文数字(例:1转为一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//数字转中文toChinesNum(num) {let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']let unit = ['', '十', '百', '千', '万']num = parseInt(num)let getWan = (temp) => {let strArr = temp.toString().split('').reverse()let newNum = ''let newArr = []strArr.forEach((item, index) => {newArr.unshift(item === '0' ? changeNum[item] : changeNum[item] + unit[index])})let numArr = []newArr.forEach((m, n) => {if (m !== '零') numArr.push(n)})if (newArr.length > 1) {newArr.forEach((m, n) => {if (newArr[newArr.length - 1] === '零') {if (n <= numArr[numArr.length - 1]) {newNum += m}} else {newNum += m}})} else {newNum = newArr[0]}return newNum}let overWan = Math.floor(num / 10000)let noWan = num % 10000if (noWan.toString().length < 4) {noWan = '0' + noWan}return overWan ? getWan(overWan) + '万' + getWan(noWan) : getWan(num)},
//调用方法console.log(toChinesNum(1)) //输出为:一
这篇关于JS阿拉伯数字转为中文数字(例:1转为一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!