Last active
April 28, 2021 23:59
-
-
Save aybabtme/61979f60148451040990152623478686 to your computer and use it in GitHub Desktop.
This file contains 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
const magnitudes = [ | |
{magnitude: 1_0000_0000, suffix: "억"}, | |
{magnitude: 1_0000, suffix: "만"}, | |
// {magnitude: 1000, suffix: "천"}, // this works too | |
]; | |
const krFormat = (num: number): string => { | |
const m = magnitudes.find((m) => num >= m.magnitude); | |
if (!m) { | |
return `${num}` | |
} | |
const v = num / m.magnitude; | |
return `${v}${m.suffix}` | |
}; | |
[ | |
1, | |
10, | |
100, | |
1_000, | |
10_000, | |
100_000, | |
1_000_000, | |
10_000_000, | |
100_000_000, | |
1_000_000_000, | |
].forEach((v) => { | |
console.log(`${v} = ${krFormat(v)}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run
Output
...or with the
{magnitude: 1000, suffix: "천"}
commented out (seems less usual):