joshcomley
joshcomley

Reputation: 28818

Is there a good javascript snippet anyone knows for formatting "abbreviated" numbers?

The key is abbreviated. For example, 1m instead of 1000000, and 12k instead of 12000 etc. - much like on StackOverflow!

I'm not sure what else to add other than I've tried:

format numbers abbreviated javascript
format numbers short javascript

And a few other searches and scoured the results with no luck. I feel like someone must have done this before, hate reinventing wheels and all that!

Cheers

Edit: I'm looking to take a number, i.e. 12345 and turn it into 12k

Sorry I wasn't very clear!

Upvotes: 0

Views: 906

Answers (3)

Nathan Friedly
Nathan Friedly

Reputation: 8146

I made an npm package that can do this for you: https://www.npmjs.com/package/approximate-number

Usage for Node.js (or browsers via Browserify):

npm install --save approximate-number

and then in your code:

var approx = require('approximate-number');
approx(123456); // "123k" 

Or, for usage in browsers via Bower:

bower install approximate-number

And then

window.approximateNumber(123456); // "123k" 

Upvotes: 1

ThiefMaster
ThiefMaster

Reputation: 318498

Here's some code I've written quite some time ago but it works fine. It even supports decimals.

function is_numeric(string) {
    for(var i = 0; i < string.length; i++) {
        if(string.charAt(i) < '0' || string.charAt(i) > '9') {
            return false;
        }
    }
    return true;
}

function charValueMultiplier(letter) {
    switch(letter) {
        case 'M':
        case 'm': return 1000000;
        case 'k':
        case 'K': return 1000;
        default: return 0;
    }
}

// parse string like 1.5M or 10k and return the number
function parseNumber(string) {
    string = string.replace(/ /g, ''); // remove spaces
    var total           = 0;
    var partial         = 0;
    var partialFraction = 0;
    var fractionLength  = 0;
    var isFraction      = false;

    // process the string; update total if we find a unit character
    for(var i = 0; i < string.length; i++) {
        var c = string.substr(i, 1);
        if(c == '.' || c == ',') {
            isFraction = true;
        }
        else if(is_numeric(c)) {
            if(isFraction) {
                partialFraction = partialFraction * 10 + parseInt(c, 10);
                fractionLength++;
            }
            else {
                partial = partial * 10 + parseInt(c, 10);
            }
        }
        else {
            total += charValueMultiplier(c) * partial + charValueMultiplier(c) * partialFraction / Math.pow(10, fractionLength);

            partial         = 0;
            partialFraction = 0;
            fractionLength  = 0;
            isFraction      = false;
        }
    }

    return Math.round(total + partial + partialFraction / Math.pow(10, fractionLength));
}

Upvotes: 4

Boldewyn
Boldewyn

Reputation: 82734

If I understand correctly, you have a number n and want to format it to a string. Then

// n being the number to be formatted
var s = "" + n; // cast as string
if (n >= 1000000) {
  s = s.substring(0, s.length - 6) + "M";
} else if (n >= 1000) {
  s = s.substring(0, s.length - 3) + "k";
}

should do the job. You can of course extend it to your needs or even include numbers < 1.

Upvotes: 0

Related Questions