Skip to content

Instantly share code, notes, and snippets.

@minedun6
Created July 7, 2022 15:40
Show Gist options
  • Save minedun6/c51b44a5184632393df4b2ea21b7d8ec to your computer and use it in GitHub Desktop.
Save minedun6/c51b44a5184632393df4b2ea21b7d8ec to your computer and use it in GitHub Desktop.
Simplify Sizes (js)
// 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