I'm trying to accomplish three things -
I want to shorten large numbers and add a K/M/B suffix I want to be able to force the number of decimal places I want to be able to force thousands to be represented as decimal fractions of a million
just shorten, round to 2 decimals
shorten, force 2 decimal places
shorten, force 3 decimal places, force thousands to millions
I have a javascript function that I found that does much of this, except that it doesn't force the number of decimal places, and it doesn't allow me to force thousands to millions
function shortenNumber (num, decimalPlaces) {
var str,
suffix = '';
decimalPlaces = decimalPlaces || 0;
num = +num;
var factor = Math.pow(10, decimalPlaces);
//99999 -> 99.9K
if (num < 1000) {
str = num;
} else if (num < 1000000) {
str = Math.floor(num / (1000 / factor)) / factor;
suffix = 'K';
} else if (num < 1000000000) {
str = Math.floor(num / (1000000 / factor)) / factor;
suffix = 'M';
} else if (num < 1000000000000) {
str = Math.floor(num / (1000000000 / factor)) / factor;
suffix = 'B';
} else if (num < 1000000000000000) {
str = Math.floor(num / (1000000000000 / factor)) / factor;
suffix = 'T';
}
return str + suffix;
}
So it accomplishes the first requirement, partially accomplishes the second (it will round to 2 decimal places, but if the decimal is 0 it drops it), but can't represent K's as M's
How can I modify this function to do this (or replace it with another that does)?
Thanks!
This should do what you are asking:
function abbreviate(number, maxPlaces, forcePlaces, forceLetter) {
number = Number(number)
forceLetter = forceLetter || false
if(forceLetter !== false) {
return annotate(number, maxPlaces, forcePlaces, forceLetter)
}
var abbr
if(number >= 1e12) {
abbr = 'T'
}
else if(number >= 1e9) {
abbr = 'B'
}
else if(number >= 1e6) {
abbr = 'M'
}
else if(number >= 1e3) {
abbr = 'K'
}
else {
abbr = ''
}
return annotate(number, maxPlaces, forcePlaces, abbr)
}
function annotate(number, maxPlaces, forcePlaces, abbr) {
// set places to false to not round
var rounded = 0
switch(abbr) {
case 'T':
rounded = number / 1e12
break
case 'B':
rounded = number / 1e9
break
case 'M':
rounded = number / 1e6
break
case 'K':
rounded = number / 1e3
break
case '':
rounded = number
break
}
if(maxPlaces !== false) {
var test = new RegExp('\\.\\d{' + (maxPlaces + 1) + ',}$')
if(test.test(('' + rounded))) {
rounded = rounded.toFixed(maxPlaces)
}
}
if(forcePlaces !== false) {
rounded = Number(rounded).toFixed(forcePlaces)
}
return rounded + abbr
}
abbreviate(1200000, 2, false, false)
abbreviate(1248000, 2, false, false)
abbreviate(248000, 2, false, false)
abbreviate(1200000, 2, 2, false)
abbreviate(1248000, 2, 2, false)
abbreviate(248000, 2, 2, false)
abbreviate(1200000, 3, 3, 'M')
abbreviate(1248000, 3, 3, 'M')
abbreviate(248000, 3, 3, 'M')
If you're not opposed to an external library, there's Numeral.js.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With