Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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



Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: