I am using the following Javascript to display my Instagram follower count on my site.
function intlFormat(num)
{
return new Intl.NumberFormat().format(Math.round(num*10)/10);
}
function makeFriendly(num)
{
if(num >= 1000000)
return intlFormat(num/1000000)+'M';
if(num >= 1000)
return intlFormat(num/1000)+'k';
return intlFormat(num);
}
Yields:
makeFriendly(1234567)
"1.2M"
makeFriendly(123457)
"123.5k"
makeFriendly(1237)
"1.2k"
makeFriendly(127)
"127"
Intl is the Javascript standard 'package' for implemented internationalized behaviours. Intl.NumberFormatter is specifically the localized number formatter. So this code actually respects your locally configured thousands and decimal separators.