function shortNumber(val) {
const abs = Math.abs(val);
const prefixIndex = Math.log10(abs) / 3 | 0;
return (
(val < 0 ? '-' : '') +
Math.round(abs / (10 ** (prefixIndex * 3))) +
'KMGTPEZY'.charAt(~-prefixIndex)
);
}
shortNumber(99) // '99'
shortNumber(1945) // '2K'
shortNumber(-5839465) // '-6M'
shortNumber(7e10) // '70G'
function intToString(value) {
var suffixes = ["", "k", "m", "b","t"];
var suffixNum = Math.floor((""+value).length/3);
var shortValue = parseFloat((suffixNum != 0 ? (value / Math.pow(1000,suffixNum)) : value).toPrecision(2));
if (shortValue % 1 != 0) {
var shortNum = shortValue.toFixed(1);
}
return shortValue+suffixes[suffixNum];
}
// 19k
alert( intToString(19410) )