Jquery-numberformatter Plug-in

Source: Internet
Author: User
Tags locale

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

Non-jquery version: Https://github.com/andrewgp/jsNumberFormatter

Example #1
Here's a typical use case for the What I ' m describing. You have an input field of your Web application that asks a person for their salary. In the US, the user can type in a varied forms of input-"$65000", "65,000", "65000", "65,000.00". All these numbers is exactly the same, but we want-control how these numbers look on the screen.

Here's an example of what you ' d use this plugin.

$ ("#salary"). blur (function () {
$ (this). Parsenumber ({format: "#,###.00", Locale: "Us"});
$ (this). FormatNumber ({format: "#,###.00", Locale: "Us"});
});

This code would ensure that any text in the "salary" TextField would be a 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 would automatically Format the number to is "65,000.00".

Example #2
Say we have 2 text input fields, one accepts US format numbers, and the other unformatted numbers only. When the user loses focus is on the formatted input it parses the data and puts the number to the second input when the US Er loses focus on the second input it formats the number back to the first input box. This was just to demonstrate how to parse and format values.

$ ("#salaryUS"). blur (function () {
Take US format text to 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 is dozens of countries supported. The syntax for the formatting follows it in the Java decimalformatter and so that can provide a reliable format string On the server and client. Syntax

The syntax for the formatting is:

    • 0 = Digit, zero shows as absent
    • # = Digit
    • . = Decimal Separator
    • -= Negative Sign
    • , = Grouping Separator
    • % = Percent (multiplies number by 100)
Supported Locales

Here is the supported Locales. They were chosen because a) they is offered by the Java Decimalformatter or b) I just felt that they were interesting and Wanted to include them.

    • states, US
    • Arab Emirates, "ae"
    • Egypt, "eg"
    • Israel- "Il"
    • Japan, JP,
    • South Korea, "KR"
    • Thailand, "th"
    • China-- "cn"
    • Hong Kong, "HK"
    • Taiwan, "TW"
    • Australia, "au"
    • Canada- "Ca"
    • great Britain, "GB"
    • India, "in"
    • Germany, "de"
    • Vietnam-> ; "VN"
    • Spain "es"
    • Denmark "DK"
    • Austria "at"
    • Greece "GR"
    • Brazil, "BR"
    • Czech "CZ"
    • France, Fr "
    • Finland," fi "
    • Russia, Ru
    • Sweden, se
    • Switzerland, "ch"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.