String.format for JavaScript

I really enjoy C#'s String.Format method and I noticed a few javascript implementations floating around the internet but most of them didn't handle curly braces in the formatting string very well and one feature they all lacked was formatting options. In C# you could use {0:h} to send 'h' to the ToString method for special formatting options.

Luckily, all JavaScript objects have a toString method used for string conversion that we can modify. Well it's not quite that simple with native objects; with native objects their built-in toString method never gets overridden but you can still invoke your custom method if you know to look for it. So in my implementation of String.format I hook into on of two methods in an object's prototype: either formatString or toString in that order of preference. (The function looks for formatString first to allow developers to work with this function without overriding native code.)

One interesting side effect of this implementation is that because Number.toString in JavaScript already supports an argument which specifies the base of the output, you can do something like {0:16} to get the hex representation of a number in a format string. But beyond this, you can write a formatString method on any object's prototype to add new formatting strings; the one caveat here is that if you write your own method and still want the native formatting features you have to re-implement them in your new method.

One example of an advanced formatting method for numbers is:

Number.prototype.formatString = function(format) {
  if (/^[2-9]|[1-2][0-9]|3[0-6]$/.test(format)) {
    // radix outputting (2-36)
    return this.toString(Number(format));
  } else if (!format) {
    return this.toString();
  } else {
    // Write your own damn number formatter!
  }
};

Before updating the code, I decided I might want to test it. So this code has had its basic feature set tested in Firefox 3, Opera 9.25, IE 6, and Safari 3.04 Windows. If a user discovers any edge cases feel free to contact me through my email , through the contact form on this site, or through the comments below. There is also a simple demo available that I may or may not make awesome later on. String.format is freely available with no guarantees or restrictions. My code additionally adds a format method to the String prototype but you can delete that if you wish.

Comments

format with pattern

Hi,
C# can format strings like that

string.Format("{0:#,##0.00}", 20000);
result 20,000.00

basically using a given pattern

Just wondering if you know any way to do that in javascript. Your current function just allows {0:h} with limited values.

Thanks a lot
G

I've updated the script

I've recently updated the script to enable those types of features. I've updated the page with the details.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Captcha
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
6 + 12 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.