|
Project Information
Members
Featured
Downloads
|
Current Release: 1.2.2 Requires jQuery and jshashtable This plugin is a number formatting and parsing plugin for jQuery. Number formatting is likely familiar to anyone who's worked with server-side code like Java or PHP and who has worked with internationalization. People who aren't stuck in the US-centric frame of mind can quickly point out that people in other parts of the world don't format their numbers in the same way as Americans. For example, a number that we would write in the US as "1,250,500.75" would be written differently in different countries: "1.250.500,75" in Germany, "1 250 500,75" in France, and "1'250'500.75" in Switzerland, and "125,0500.75" in Japan. The number is exactly the same, but it's just written using a different format when presented to users of the web application. I've been working during the 1.2 rewrite to separate the parsing and formatting more. So your now required to parse the text first into a js number, before formatting it back into text. This should hopefully give more control over the process and allow more flexibility of use. Example #1 Here's an example of how you'd use this plugin. $("#salary").blur(function(){
$(this).parseNumber({format:"#,###.00", locale:"us"});
$(this).formatNumber({format:"#,###.00", locale:"us"});
});This code will ensure that any text in the "salary" textfield will be formatted properly when the user tabs out of it. For example, the user can enter "65000", "65,000", "65000.00" and when they leave the field, the field will automatically format the number to be "65,000.00". Example #2 $("#salaryUS").blur(function(){
// take US format text into std number format
var number = $(this).parseNumber({format:"#,###.00", locale:"us"}, false);
// write the number out
$("#salaryUnformatted").val(number);
// OR
number = $(this).val();
number = $.parseNumber(number, {format:"#,###.00", locale:"us"});
$("#salaryUnformatted").val(number);
});
$("#salaryUnformatted").blur(function(){
// take the unformatted text and format into US number format
$("#salaryUS").val($(this).val());
$("#salaryUS").formatNumber({format:"#,###.00", locale:"us"});
// OR
var number = $(this).val();
number = $.formatNumber(number, {format:"#,###.00", locale:"us"});
$("#salaryUS").val(number);
}); Right now there are dozens of countries supported. The syntax for the formatting follows that in the Java DecimalFormatter, so that you can provide a reliable format string on the server and client. SyntaxThe syntax for the formatting is:
Supported LocalesHere are the supported Locales. They were chosen because a) they are offered by the Java DecimalFormatter or b) I just felt that they were interesting and wanted to include them.
MentionsThanks to the excellent jshashtable project, which is currently a requirement of the script, I may decide to support a standalone version too at some point. SNAPSHOTSAvailable from the svn repo (https://jquery-numberformatter.googlecode.com/svn/trunk), please take care to read the notes in the main js file, may be unstable or incomplete. 1.2.2
1.2.1
1.2New/Fixes
New in Version 1.1.21 major bug fixes, 1 minor fix, and 1 new feature included in this release. The bug fixes are
New in Version 1.1I recommend everyone to move to the 1.1 version of the plugin, as it is more stable and contains more features.
|