Skip to content

Instantly share code, notes, and snippets.

@dipeshdulal
Created August 27, 2020 09:17
Show Gist options
  • Save dipeshdulal/fec0e881a3c94330476ef15be093c207 to your computer and use it in GitHub Desktop.
Save dipeshdulal/fec0e881a3c94330476ef15be093c207 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