I have made an idle clicker game where you can click the dollar bill and get money and upgrade etc. I've just created an abbreviateNumber function where it shortens values over 1000 to 1k, 1m, 1t etc. I copied it off this previous thread: Convert long number into abbreviated string in JavaScript, with a special shortness requirement , but I'm not sure how to display with 2 decimal places rather than just 1, e.g. 1.05k, 2.65M etc.
I'd be grateful if someone could help me so I can carry on coding my game, my abbreviateNumber function is below:
function abbreviateNumber(value) {
var newValue = value;
if (value >= 1000) {
var suffixes = ["", "k", "m", "b","t"];
var suffixNum = Math.floor( (""+value).length/3 );
var shortValue = '';
for (var precision = 2; precision >= 1; precision--) {
shortValue = parseFloat( (suffixNum != 0 ? (value / Math.pow(1000,suffixNum) ) : value).toPrecision(precision));
var dotLessShortValue = (shortValue + '').replace(/[^a-zA-Z 0-9]+/g,'');
if (dotLessShortValue.length <= 2) { break; }
}
if (shortValue % 1 != 0) shortNum = shortValue.toFixed(1);
newValue = shortValue+suffixes[suffixNum];
}
return newValue;
}