Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darraghoriordan/db9b34a0a94126adc9edabfc2dd37df9 to your computer and use it in GitHub Desktop.
Save darraghoriordan/db9b34a0a94126adc9edabfc2dd37df9 to your computer and use it in GitHub Desktop.
social number formatter
function numberToSocial(bigNum, startAt = 10_000) {
return bigNum < startAt ? bigNum : Intl.NumberFormat("en", {
notation: "compact",
style: "decimal",
maximumFractionDigits: 1,
roundingMode: "floor",
unitDisplay: "narrow",
}).format(bigNum)
}
console.log(numberToSocial(123));
console.log(numberToSocial(1457));
console.log(numberToSocial(10001));
console.log(numberToSocial(10512));
console.log(numberToSocial(2_300_213));
console.log(numberToSocial(1457, 1000));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment