js views number to short version

Solutions on MaxInterview for js views number to short version by the best coders in the world

showing results for - "js views number to short version"
Jakob
11 Jun 2019
1function abbreviateNumber(value) {
2    var newValue = value;
3    if (value >= 1000) {
4        var suffixes = ["", "k", "m", "b","t"];
5        var suffixNum = Math.floor( (""+value).length/3 );
6        var shortValue = '';
7        for (var precision = 2; precision >= 1; precision--) {
8            shortValue = parseFloat( (suffixNum != 0 ? (value / Math.pow(1000,suffixNum) ) : value).toPrecision(precision));
9            var dotLessShortValue = (shortValue + '').replace(/[^a-zA-Z 0-9]+/g,'');
10            if (dotLessShortValue.length <= 2) { break; }
11        }
12        if (shortValue % 1 != 0)  shortValue = shortValue.toFixed(1);
13        newValue = shortValue+suffixes[suffixNum];
14    }
15    return newValue;
16}