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
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.
When use this Microsoft
When use this Microsoft Office 2007 Download you can create a digital Microsoft Office 2010 notebook in Microsoft Office 2011 to organize your ideas Office 2007 Key information for any Download Office 2007 project, from a home remodel Microsoft Office 2007 Professional to a major purchase decision.
Download Office 2010 and share all the materials and Office 2010 Key media for a Microsoft Office 2010 Download project in an Office 2010 Download: Windows 7, Microsoft Windows 7, Win 7, Download Windows 7, Buy Windows 7, and more. At the same time Office 2007 may find the information Microsoft Office 2007 you need with simple Office 2007 Download tagging and search functions and Office 2007 Professional take notes as you work. In MS Office 2007 addition the notes link automatically buy microsoft office 2007 to the page purchase microsoft office 2007 working on in the other Office 2010 application so that MS Office 2010 you can easily Office 2010 Professional search and find buy microsoft office 2010 them.
Post new comment