36

Much like the Stackoverlow reputation rounding, I'm hoping to do the same thing with currency

$1,000 => 1k

$1,000,000 => 1m

How can I achieve this in JavaScript (preferably in jQuery)?

Max
  • 386
  • 3
  • 16
Baloneysammitch
  • 681
  • 2
  • 6
  • 11
  • 1
    This currently doesn't qualify as an acceptable code-golf question. see http://meta.stackexchange.com/questions/24258 You need a few more test cases, to make it community wiki and finally you should accept the shortest (in code size) answer. Single language code-golfs are boring, you should also open it to other languages – John La Rooy Apr 21 '10 at 23:22
  • David created a [code-golf] version: http://stackoverflow.com/questions/2692323/code-golf-friendly-number-abbreviator – dmckee --- ex-moderator kitten Apr 22 '10 at 16:22
  • 2
    Thanks guys -- as you can see I'm new to Stack Overflow. I'm excited that you all think this is an interesting problem! – Baloneysammitch Apr 23 '10 at 21:44

2 Answers2

122

Here is a simple function to do it:

function abbrNum(number, decPlaces) {
    // 2 decimal places => 100, 3 => 1000, etc
    decPlaces = Math.pow(10,decPlaces);

    // Enumerate number abbreviations
    var abbrev = [ "k", "m", "b", "t" ];

    // Go through the array backwards, so we do the largest first
    for (var i=abbrev.length-1; i>=0; i--) {

        // Convert array index to "1000", "1000000", etc
        var size = Math.pow(10,(i+1)*3);

        // If the number is bigger or equal do the abbreviation
        if(size <= number) {
             // Here, we multiply by decPlaces, round, and then divide by decPlaces.
             // This gives us nice rounding to a particular decimal place.
             number = Math.round(number*decPlaces/size)/decPlaces;

             // Handle special case where we round up to the next abbreviation
             if((number == 1000) && (i < abbrev.length - 1)) {
                 number = 1;
                 i++;
             }

             // Add the letter for the abbreviation
             number += abbrev[i];

             // We are done... stop
             break;
        }
    }

    return number;
}

Outputs:

abbrNum(12 , 1)          => 12
abbrNum(0 , 2)           => 0
abbrNum(1234 , 0)        => 1k
abbrNum(34567 , 2)       => 34.57k
abbrNum(918395 , 1)      => 918.4k
abbrNum(2134124 , 2)     => 2.13m
abbrNum(47475782130 , 2) => 47.48b

Demo: http://jsfiddle.net/jtbowden/SbqKL/

Jeff B
  • 29,943
  • 7
  • 61
  • 90
  • 3
    this feels golfy. how small can we get this function? – David Murdoch Apr 21 '10 at 19:56
  • FUND IT. Seriously though, I'd love to see that in a golf thread. – Damien Wilson Apr 21 '10 at 20:17
  • @ Damien Wilson: Code-golf tag added. What do you mean by fund? – Baloneysammitch Apr 21 '10 at 20:39
  • @Baloney: I was just making it flexible. Do what is best for your application. An "appropriate cut-off" point is often subjective and/or application specific. – Jeff B Apr 21 '10 at 20:44
  • Code golf version posted [here](http://stackoverflow.com/questions/2692323/code-golf-number-formatter). For contributing members of OP question: feel free to change the parameters of the question...I had a difficult time deciding the limitations/boundaries. – David Murdoch Apr 22 '10 at 15:51
  • 7
    `function a(n,d){x=(''+n).length,p=Math.pow,d=p(10,d);x-=x%3;return Math.round(n*d/p(10,x))/d+" kMGTPE"[x/3]}` --- Sufficiently Golfed (108)? :) – gnarf May 21 '10 at 20:34
  • 1
    `abbrNum(999950, 0) => 1000k`, yeah it only happens 50 / 1M times but still. – crizCraig Aug 18 '12 at 22:52
  • 1
    @crizCraig: Good catch! I added a special case handler for that in the code. There is probably a more efficient way to do it, but it works for now. – Jeff B Aug 30 '12 at 17:02
  • **104 bytes** `function(a,b,c,d){c=(''+a).length;d=Math.pow;b=d(10,b);return((a*b/d(10,c-=c%3))+.5|0)/b+' kMGTPE'[c/3]}` – yckart Oct 21 '13 at 23:23
  • 2
    http://stackoverflow.com/a/25612036/1729885 – Niels Keurentjes Sep 01 '14 at 19:39
  • I have an optimised version of the above code in objective C over here. http://stackoverflow.com/a/35742809/1920799. ... .... @JeffB : your method doesn't work for negative numbers. you need to use the following ..... if(size <= Math.abs(Math.round(number))) ..... instead of ..... if(size <= number) ......... Which has an extra benefit of removing your code block of 'Handle special case where we round up to the next abbreviation' – Rajesh Mar 02 '16 at 09:16
  • can without round ? abbrNum(1432, 1); => 1.4k; abbrNum(1992, 1); => 1.9k – padavan Feb 06 '22 at 09:08
  • @padavan Just change the `Math.round` above to `Math.floor`. – Jeff B Feb 07 '22 at 20:18
24
var floor=Math.floor, abs=Math.abs, log=Math.log, round=Math.round, min=Math.min;
var abbrev = ['k', 'Mil', 'Bil']; // abbreviations in steps of 1000x; extensible if need to edit

function rnd(n, precision) {
    var prec = 10**precision;
    return round(n*prec)/prec;
}

function format(n) {
    var base = floor(log(abs(n))/log(1000));
    var suffix = abbrev[min(abbrev.length-1, base-1)];
    base = abbrev.indexOf(suffix) + 1;
    return suffix ? rnd(n/1000**base,2)+suffix : ''+n;
}

Demo:

> tests = [-1001, -1, 0, 1, 2.5, 999, 1234, 
           1234.5, 1000001, 10**9, 10**12]
> tests.forEach(function(x){ console.log(x,format(x)) })

-1001 "-1k"
-1 "-1"
0 "0"
1 "1"
2.5 "2.5"
999 "999"
1234 "1.23k"
1234.5 "1.23k"
1000001 "1Mil"
1000000000 "1Bil"
1000000000000 "1000Bil"
ninjagecko
  • 88,546
  • 24
  • 137
  • 145
  • This one is the best since it works with negative numbers too –  May 29 '19 at 14:08
  • @shrekuu: seems to be passed just fine as `round`ing to two decimal places in `round(n/pow(...), 2)`; feel free to clarify – ninjagecko Nov 17 '22 at 23:34
  • Oh sorry my bad. Failed to read it more carefully. I will delete my previous comment to avoid misleading. – shrekuu Nov 18 '22 at 16:04