0

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!

joshcomley
  • 28,099
  • 24
  • 107
  • 147
  • 12k may also be 12288 - http://en.wikipedia.org/wiki/Kilobyte – Boldewyn Jun 24 '11 at 10:19
  • You could have a look at [this](http://stackoverflow.com/questions/3177855/how-to-format-numbers-similar-to-stack-overflow-reputation-format) or [this](http://stackoverflow.com/questions/2134161/format-number-like-stackoverflow-rounded-to-thousands-with-k-suffix) and reverse it... :) – Felix Kling Jun 24 '11 at 10:20
  • Do you want `1000 -> 1k` or `1k -> 1000` ? – Felix Kling Jun 24 '11 at 10:26

3 Answers3

4

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));
}
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • I think you should add a `g` to `string.replace(/ /, '')` but other than that, I believe you ;) +1 – Felix Kling Jun 24 '11 at 10:24
  • hi new to js @ThiefMaster - is this easily able to be convert large (bigint) numbers like Trillion 10^12 (T) Quadrillion 10^15 (P) and Quintillion 10^18 (E) - if so can you walk me through it please if you have time – soulshined Sep 16 '16 at 04:24
1

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" 
Nathan Friedly
  • 7,837
  • 3
  • 42
  • 59
0

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.

Boldewyn
  • 81,211
  • 44
  • 156
  • 212