Skip to content

Instantly share code, notes, and snippets.

@dipeshhkc
Forked from dipeshdulal/minimize.js
Created August 27, 2020 13:28
Show Gist options
  • Save dipeshhkc/43e763ebae7e7ba3f533a5e60126b546 to your computer and use it in GitHub Desktop.
Save dipeshhkc/43e763ebae7e7ba3f533a5e60126b546 to your computer and use it in GitHub Desktop.
Minimize Number into K, M, G, T, P, E
var minimizeNumber = (num) => {
if(!Number.isFinite(num)) return "";
const minimizeMap = ["K", "M", "G", "T", "P", "E"];
if(num < 1000) return num.toString();
const exp = Math.floor(Math.log(num)/ Math.log(1000));
const minimized = Number((num / Math.pow(1000, exp)).toFixed(2));
return minimized+minimizeMap[exp-1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment