当前位置:网站首页>js中把数字转换成汉字输出
js中把数字转换成汉字输出
2022-07-01 00:39:00 【coldriversnow】
前言:
在js中把数字转换成汉字输出的方法,直接可以拿来用
方法一:支持7位,也就是最大1234567
案例:this.toChinesNum(10101010) 得到 "一千零一十万一千零一十"
/**
* 数字转成汉字
* @params num === 要转换的数字
* @return 汉字
* */
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 % 10000
if (noWan.toString().length < 4) {
noWan = '0' + noWan
}
return overWan ? getWan(overWan) + '万' + getWan(noWan) : getWan(num)
}
调用: let a = this.toChinesNum(1) 这里的a拿到的值就是一
方法二:支持9位以上也就是亿级别的,如果需要钱的那种单位,把注释放开就行
toChineseBig(num) {
// 将接收到的num转换为字符串
var strNum = String(num)
// 定义单位
// var unit = ['拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟']
var unit = ['十', '百', '千', '万', '十', '百', '千', '亿', '十', '百', '千']
// 结果中放一个符号,用来解决最后的零去不掉的问题
var result = ['@']
// 单位下标
var unitNo = 0
// 从后往前遍历接收到的数据,省略结束条件
for (let i = strNum.length - 1;; i--) {
// 调用转大写函数,将每一个数字转换成中文大写,一次放入一个到结果数组中
result.unshift(numToChinese(strNum[i]))
// 如果不大于0
if (i <= 0) {
// 结束循环
break
}
// 放入一个数字,放入一个单位
result.unshift(unit[unitNo])
// 单位下标加1
unitNo++
}
// 将结果数组转换成字符串,并使用正则替换一些关键位置,让结果符合语法
// return result.join('').replace(/(零[仟佰拾]){1,3}/g, '零').replace(/零{2,}/g, '零').replace(/零([万亿])/g, '$1').replace(/亿万/g, '亿').replace(/零*@/g, '')
return result.join('').replace(/(零[千百十]){1,3}/g, '零').replace(/零{2,}/g, '零').replace(/零([万亿])/g, '$1').replace(/亿万/g, '亿').replace(/零*@/g, '')
function numToChinese(n) {
// var chineseBigNum = '零壹贰叁肆伍陆柒捌玖'
var chineseBigNum = '零一二三四五六七八九'
return chineseBigNum[n]
}
}
}
调用: let a = this.toChineseBig(112345111111) 这里的a拿到的值就是 一千一百二十三亿四千五百一十一万一千一百一十一
边栏推荐
- The longest selling mobile phone in China has been selling well since its launch, crushing iphone12
- 软硬件基础知识学习--小日记(1)
- 蒹葭苍苍,白露为霜。
- 二十多年来第一次!CVPR最佳学生论文授予中国高校学生!
- Service
- 2022 is half way through. It's hard to make money
- NE555波形发生器手把手教程之NE555内部结构(一)
- Hoo research | coinwave production - nym: building the next generation privacy infrastructure
- Oracle data integrity
- The question of IBL precomputation is finally solved
猜你喜欢
The quantity and quality of the devil's cold rice 101; Employee management; College entrance examination voluntary filling; Game architecture design
Analysis of blocktoken principle
DLS-20型双位置继电器 220VDC
What if the disk of datanode is full?
Set different background colors for the border and text of the button
Two-stage RO: part 1
PyTorch安装并使用gpu加速
Oracle临时表详解
探索互联网时代STEAM教育创新之路
写给 5000 粉丝的一封信!
随机推荐
女朋友说:你要搞懂了MySQL三大日志,我就让你嘿嘿嘿!
New content violation degree determination scana bad information monitoring capability update issue 5
What if the disk of datanode is full?
第53章 从业务逻辑实现角度整体性理解程序
人穷志不短,穷学生也能玩转树莓派
C#生成putty格式的ppk文件(支持passphrase)
【学习笔记】简单dp
The quantity and quality of the devil's cold rice 101; Employee management; College entrance examination voluntary filling; Game architecture design
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
Oracle data integrity
Stack frame
Tibetan poem PTA
Analysis of blocktoken principle
[LeetCode] 两数之和【1】
【2023联发科提前批笔试题】~ 题目及参考答案
双链表:初始化 插入 删除 遍历
Oracle临时表详解
Practical shell knowledge
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
K210门禁毕设