Hey all! If you ever worked in fintech - or any other number-related field - you probably came across a requirement like this:
Format a number to the abbreviated version but only if that-and-that criteria are met.
I had such a requirement and the criteria were something like this:
- If a number is less than 1000 show as-is, and no decimal points
- If a number is greater than 1000, abbreviate it to the corresponding string value e.g. 1K, and always use two decimal points e.g. 1233 -> 1.23K.
- If a number is greater than a million do it like 1M, 1.33M
- If a number is a billion do it like 1B, 10.45B, 123.12B
- Same goes for a trillion
Here is a simple, short, and sweet solution, and If you tweak it right you can accommodate your own criteria.
Hope this helps you in your future work. Cheers! 🍻