Created
May 31, 2017 00:57
Abbreviate large numbers in Javascript
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
// Iterated from: https://stackoverflow.com/questions/10599933/convert-long-number-into-abbreviated-string-in-javascript-with-a-special-shortn | |
function abbreviateNumber(value) { | |
let newValue = value; | |
const suffixes = ["", "K", "M", "B","T"]; | |
let suffixNum = 0; | |
while (newValue >= 1000) { | |
newValue /= 1000; | |
suffixNum++; | |
} | |
newValue = newValue.toPrecision(3); | |
newValue += suffixes[suffixNum]; | |
return newValue; | |
} |
Thanks nice one.
if number is 12 then it be 12.0
or 1 it will be 1.0
you can change it to:
newValue = newValue.toString().length > 2 ? newValue.toPrecision(3) : newValue.toPrecision();
Thanks a lot it was very helpful.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple and Perfect answer! Thanks!