how to abreviate digits in js

Solutions on MaxInterview for how to abreviate digits in js by the best coders in the world

showing results for - "how to abreviate digits in js"
Simona
17 Aug 2017
1function nFormatter(num) {
2     if (num >= 1000000000) {
3        return (num / 1000000000).toFixed(1).replace(/\.0$/, '') + 'G';
4     }
5     if (num >= 1000000) {
6        return (num / 1000000).toFixed(1).replace(/\.0$/, '') + 'M';
7     }
8     if (num >= 1000) {
9        return (num / 1000).toFixed(1).replace(/\.0$/, '') + 'K';
10     }
11     return num;
12}
Michela
25 Apr 2020
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}
queries leading to this page
number to k m b javascriptshort form from integers in days javascriptconvert number to k m b javascripthow to convert a number to k value in javascriptconvert long number into abbreviated string in javascript 2c with a special shortness requirementshorthand numbers plus 1 javascriptjavascript how to make ints not use 1e 2b shortcutnode place long data into short stringconvert chain of characters to short number javascripthow to convert int no to kg in jshow to abreviate digits in jsprice formatter jsnumber k m b t to normal number javascriptjavascript convert registration to 100 25kthousand million abbreviation javascriptconvert number to k and m javascriptnumber string shortener javascript millionnum abbreviation jsjs abbreviate large numbercurrency abbreviations javascriptnumber to smaller number name jsjavascript convert number to k or mconvert number to k and m in javascriptconvert big numbers to letters nodejsjavascript compress long number to abbreviationappreviation to long number jsconvert abbreviation number jsjquery string to number short formnumber to k m b converter javascript codejavascript numerals add k with 4 digitscreate thousands 2c and abbreviations of number in jscovert numbers k m b javascriptnumeral js number doesnt have m and k in capsjavascript abbreviate numberhow to abbreviate numbers using jsprogram to convert number to k m b javascriptturn numbers into shortened notation pythonjavascript numerical libraryabbreviate number jsjs convert numbers to k mjavascript replace big numbers with lettershow to abreviate digits in js