function numberShorter(value, fixed, symbols) {
var schema = symbols || ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
var index = Math.floor(Math.log10(value) / 3);
return (value / Math.pow(10, 3 * index)).toFixed(fixed || 2) + (schema[index] === undefined ? ('e' + index * 3) : schema[index]);
}
const SHORTCUT_SUFFIXES = ['K', 'M', 'B', 'T', 'Q', 'P'];
function shortcutNumber(num) {
const normalized = Number(num).toFixed(0);
const expPosition = normalized.indexOf('e');
const dotPosition = normalized.indexOf('.');
const hasExponent = expPosition !== -1;
const exponent = parseInt(normalized.slice(expPosition + 2));
const len = hasExponent ? expPosition + exponent : normalized.length;
if(len < 4) return String(num);
const suffixNumber = Math.floor((len - 1) / 3);
const suffix = SHORTCUT_SUFFIXES[suffixNumber - 1];
const fullNumber = (hasExponent
? normalized.slice(0, expPosition).replace('.', '')
+ '0'.repeat(exponent - (dotPosition === -1 ? 0 : expPosition + dotPosition + 1))
: normalized
);
if(!suffix) {
// число о-о-очень большое, больше чем мы суффиксов задали...
return fullNumber.slice(0, -(SHORTCUT_SUFFIXES.length * 3))
+ SHORTCUT_SUFFIXES[SHORTCUT_SUFFIXES.length - 1];
}
return fullNumber.slice(0, -(suffixNumber * 3)) + suffix;
}
function shortNum(n) {
return n.toString()
.replace(/(\d)\d{15}$/, "$1z")
.replace(/(\d)\d{12}$/, "$1y")
.replace(/(\d)\d{9}$/, "$1x")
.replace(/(\d)\d{6}$/, "$1m")
.replace(/(\d)\d{3}$/, "$1k");
// вместо {n} - количество нулей
// в кавычках соответствующая буква
}
console.log(shortNum(86584959)); // 86m
function shortNum(n) {
return n.toString()
.replace(/(\d)(\d)\d{5}$/, "$1.$2m")
.replace(/(\d)(\d)\d{2}$/, "$1.$2k");
}
console.log(shortNum(9537675)); // 9.5m