Slacker News Slacker News logo featuring a lazy sloth with a folded newspaper hat
  • top
  • new
  • show
  • ask
  • jobs
Library
← Back to context

Comment by 8bitsrule

4 years ago

Overkill. KISS. n counts moving the decimal left 3 places until there are <=3 digits left. Keep one place to the right.

"BKMGTPE". n==0 => 'B'; n==4 => 'T'.

810 has 3 digits. n==0, 810B.

999950 has 6 digits. n==1, you've got 999.9K

1100000 has 7 digits. n==2, 1.1M

1234567890 has 10 digits. n==3, 1.2G

0 comments

8bitsrule

Reply

No comments yet

Contribute on Hacker News ↗

Slacker News

Product

  • API Reference
  • Hacker News RSS
  • Source on GitHub

Community

  • Support Ukraine
  • Equal Justice Initiative
  • GiveWell Charities