随机生成一个1至十万的数值,并把数值转换为中文数字。例:99999 = 九万九千九百九十九,90909=九万零九百零九
function f1(num){
let arr1 = ['万','千', '百', '十']
let arr2 = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
let str = num.toString() //数字转换为字符串
let len = str.length
let arr3 = arr1.slice(5-len)
let s = ''
let flag = false
for(let i = 0; i <= len-2; i++){
if(str[i] === '0'){
if(!flag){
s += arr2[str[i]]
flag = true
continue
}
}else{
s += arr2[str[i]] + arr3[i]
flag = false
}
}
if(str[len - 1] === '0'){
return s
}
s += arr2[str[len - 1]]
return s
}
let num = Math.floor(10000*Math.random())
console.log(num)
console.log(f1(num))
注意:一定要注意为0的数
网友评论