Front-End-Development-Notes icon indicating copy to clipboard operation
Front-End-Development-Notes copied to clipboard

阿拉伯数字转中文写法

Open lizuncong opened this issue 3 years ago • 0 comments

function transform(num){
    const unitMap = ['亿', '千', '百', '十', '万', '千', '百', '十', '']
    const numMap = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
    const arr = String(num).split('')
    const start = unitMap.length - arr.length;
    let result = ''
    arr.forEach((n, idx) => {
        result = result + numMap[n] + unitMap[start + idx]
    })
    result = result.replace('零万', '万')
    result = result.replace(/零./g, '零')
    result = result.replace(/零+$/g, '')
    console.log(result)
    return result
    
}
transform(104340000) // 一亿零四百三十四万
transform(10403040) // 一千零四十万三千零四十
transform(403040) // 四十万三千零四十

lizuncong avatar May 07 '22 06:05 lizuncong