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)}`)
})
@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