Format Numbers in JavaScript Similar to C#

How to format numbers similar to Stack Overflow reputation format

Another approach that produces exactly the desired output:

function getRepString (rep) {
rep = rep+''; // coerce to string
if (rep < 1000) {
return rep; // return the same number
}
if (rep < 10000) { // place a comma between
return rep.charAt(0) + ',' + rep.substring(1);
}
// divide and format
return (rep/1000).toFixed(rep % 1000 != 0)+'k';
}

Check the output results here.

Is there a C# String.Format() equivalent in JavaScript?

Try sprintf() for javascript.

Or

// First, checks if it isn't implemented yet.
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}

"{0} is dead, but {1} is alive! {0} {2}".format("ASP", "ASP.NET")

Both answers pulled from JavaScript equivalent to printf/string.format

jQuery (or Javascript) Way to Format a Number Based on a Given C#/VBA/Java Format String

And after sleeping on it and trying to google again the morning I believe I have found what I need.

flexible-js

This appears to be working as I would expect. Don't know if it handles every use case perfectly but based on initial testing it looks to be working as I need.

Javascript: Easier way to format numbers?

There's the NUMBERFORMATTER jQuery plugin, details below:

https://code.google.com/p/jquery-numberformatter/

From the above link:

This plugin is a NumberFormatter
plugin. Number formatting is likely
familiar to anyone who's worked with
server-side code like Java or PHP and
who has worked with
internationalization.

EDIT: Replaced the link with a more direct one.

Format Number like Stack Overflow (rounded to thousands with K suffix)

Like this: (EDIT: Tested)

static string FormatNumber(int num) {
if (num >= 100000)
return FormatNumber(num / 1000) + "K";

if (num >= 10000)
return (num / 1000D).ToString("0.#") + "K";

return num.ToString("#,0");
}

Examples:

  • 1 => 1
  • 23 => 23
  • 136 => 136
  • 6968 => 6,968
  • 23067 => 23.1K
  • 133031 => 133K

Note that this will give strange values for numbers >= 108.

For example, 12345678 becomes 12.3KK.

How to format numbers by prepending 0 to single-digit numbers?

Edit (2021):

It's no longer necessary to format numbers by hand like this anymore. This answer was written way-back-when in the distant year of 2011 when IE was important and babel and bundlers were just a wonderful, hopeful dream.

I think it would be a mistake to delete this answer; however in case you find yourself here, I would like to kindly direct your attention to the second highest voted answer to this question as of this edit.

It will introduce you to the use of .toLocaleString() with the options parameter of {minimumIntegerDigits: 2}. Exciting stuff. Below I've recreated all three examples from my original answer using this method for your convenience.

[7, 7.5, -7.2345].forEach(myNumber => {
let formattedNumber = myNumber.toLocaleString('en-US', {
minimumIntegerDigits: 2,
useGrouping: false
})
console.log(
'Input: ' + myNumber + '\n' +
'Output: ' + formattedNumber
)
})


Related Topics



Leave a reply



Submit