Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Support] Add Num.abbreviate funciton #285

Closed
kiaking opened this issue May 26, 2023 · 7 comments · Fixed by #286
Closed

[Support] Add Num.abbreviate funciton #285

kiaking opened this issue May 26, 2023 · 7 comments · Fixed by #286
Labels
enhancement New feature or request

Comments

@kiaking
Copy link
Member

kiaking commented May 26, 2023

We use this internally in GB's app and good have it in sefirot. We should add test and such. This method do things like:

Num.abbriviate(1_000, 0)     // '1K'
Num.abbriviate(1_250_000, 2) // '1.25M'
/**
 * Convert given number to abbriviated format such as `1K` or `1M`.
 */
export function abbreviate(num: number, precision = 0): string {
  const K = 1000
  const M = 1000000
  const B = 1000000000

  if (num >= K && num < M) {
    return `${(num / K).toFixed(precision)}K`
  }

  if (num >= M && num < B) {
    return `${(num / M).toFixed(precision)}M`
  }

  if (num >= B) {
    return `${(num / B).toFixed(precision)}B`
  }

  return String(num)
}
@kiaking kiaking added the enhancement New feature or request label May 26, 2023
@brc-dd
Copy link
Member

brc-dd commented May 26, 2023

Simpler way: 😅

Intl.NumberFormat('en-US', {
  notation: 'compact',
  maximumFractionDigits: 2
}).format(1_250_000);

@kiaking
Copy link
Member Author

kiaking commented May 26, 2023

OK I'm way behind on new JS stuff lol

@brc-dd
Copy link
Member

brc-dd commented May 26, 2023

Nah, this is too old. It's supported in IE too 🤣

@kiaking
Copy link
Member Author

kiaking commented May 26, 2023

Oh no... Wait, please tell me it was supported from IE 11. Because the old means IE 6 for me 😏

@brc-dd
Copy link
Member

brc-dd commented May 26, 2023

Yeah, IE 11 😅 (I wasn't even born when IE 6 was launched 😅)

@kiaking
Copy link
Member Author

kiaking commented May 26, 2023

I'm not sure if I should be happy about it 🤔

@cuebit
Copy link
Member

cuebit commented May 26, 2023

I'm not sure if I should be happy about it 🤔

Be happy! You know elegant design because you’ve design constraints from IE6 nightmares 😂

kiaking pushed a commit that referenced this issue May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants