Created
August 27, 2020 09:17
-
-
Save dipeshdulal/fec0e881a3c94330476ef15be093c207 to your computer and use it in GitHub Desktop.
Minimize Number into K, M, G, T, P, E
This file contains hidden or 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
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