数字に千・万・億などの日本語の桁を付けるJS
/**
* 数字に漢字の桁を付ける
* ex: 1234567890123 => 1兆2345億6789万123
* @param {String | Number} num 数字
* @return {String} 漢字付きの数字
*/
export default function num2ja(num) {
const keta = ['', '千', '万', '億', '兆']
let jaNum = ''
let count = 0
num = String(num).replace(',', '')
// 千
if(num.length <= 3) {
return num
}
else {
const n = num.slice(-3) - 0 + ''
jaNum = [n !== '0' ? `${n}${keta[count]}` : '', jaNum].join('')
num = (num / 1000 | 0) + ''
count++
}
// 万
if(num.length > 1) {
const n = num.slice(-1) - 0 + ''
jaNum = [n !== '0' ? `${n}${keta[count]}` : '', jaNum].join('')
num = (num / 10 | 0) + ''
count++
}
// それ以降
while(num.length > 4) {
const n = num.slice(-4) - 0 + ''
jaNum = [n !== '0' ? `${n}${keta[count]}` : '', jaNum].join('')
num = (num / 10000 | 0) + ''
count++
}
jaNum = [num !== '0' ? `${num}${keta[count]}` : '', jaNum].join('')
return jaNum
}