Skip to content

Instantly share code, notes, and snippets.

@aybabtme
Last active April 28, 2021 23:59
Show Gist options
  • Save aybabtme/61979f60148451040990152623478686 to your computer and use it in GitHub Desktop.
Save aybabtme/61979f60148451040990152623478686 to your computer and use it in GitHub Desktop.
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)}`)
})
@aybabtme
Copy link
Author

aybabtme commented Apr 28, 2021

Run

$ deno run kr_numerals.ts

Output

1 = 1
10 = 10
100 = 100
1000 = 1000
10000 = 1만
100000 = 10만
1000000 = 100만
10000000 = 1000만
100000000 = 1억
1000000000 = 10억

...or with the {magnitude: 1000, suffix: "천"} commented out (seems less usual):

1 = 1
10 = 10
100 = 100
1000 = 1천
10000 = 1만
100000 = 10만
1000000 = 100만
10000000 = 1000만
100000000 = 1억
1000000000 = 10억

@kimmike
Copy link

kimmike commented Apr 28, 2021

👍🙇‍♂️😍❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment