Created
July 7, 2022 15:40
-
-
Save minedun6/c51b44a5184632393df4b2ea21b7d8ec to your computer and use it in GitHub Desktop.
Simplify Sizes (js)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For reference | |
// https://twitter.com/mattkingshott/status/1545035927831052288/photo/1 | |
simplify(value) { | |
let regex = '/^-?\d+(?:\.\d{0,1})?/'; | |
let format = (value) => value.matche(regex)[0].replace('.0', ''); | |
// Hundred (or less) | |
if (value < 1000) { | |
return value; | |
} | |
// Thounsads | |
if (value < 1000000) { | |
return format(value / 1000) + 'K'; | |
} | |
// Millions | |
if (value < 1000000000) { | |
return format(value / 1000000) + 'M'; | |
} | |
// Billions | |
return format(value / 1000000000) + 'B'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment