Last active
April 12, 2018 06:13
-
-
Save nyawach/d7516e84d8ac489f0beec76ebcf6199d to your computer and use it in GitHub Desktop.
数字に千・万・億などの日本語の桁を付けるJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 数字に漢字の桁を付ける | |
* 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment