Click here to Skip to main content
13,551,964 members
Click here to Skip to main content


14 bookmarked
Posted 30 Nov 2010
Licenced CPOL

JavaScript Number Parsing and Formatting for Multicultural Environments

, 21 Dec 2010
A JavaScript class used to help in formatting and parsing numbers when parseInt and parseFloat are not enough
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JSTester.aspx.cs" Inherits="NumberFormatterExample.JSTester" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <script src="Scripts/NumberFormat.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="Scripts/json2.js" type="text/javascript"></script>
    <script type="text/javascript">
        var _formatted = $();
        var _unformatted = $();
        var _formatName = $();
        var _testValue = $();
        var _testFormat = $();
        var _formatter = new Formatting.NumberFormatter(new Formatting.NumberFormatInfo());

        $(function () {
            _formatted = $("#formatted .value");
            _unformatted = $("#unformatted .value");
            _formatName = $("#formatName");
            _testValue = $("#testValue");
            _testFormat = $("#testFormat");

   () { call(); });
            $("#testParse").click(function () { testParse(); });

        function call() {
            var settings = $.extend({}, $.ajaxSettings);
            settings.contentType = 'application/json; charset=utf-8';
            settings.dataType = "json";
            settings.async = false;
            settings.type = "POST";
            settings.url = "FormattingServices.svc/GetFormat";
   = JSON.stringify({ format: _formatName.val() });
            //settings.processData = false;
            settings.cache = false;
            settings.success = function (returnObject) {
                testFormat(new Formatting.NumberFormatter(new Formatting.NumberFormatInfo(returnObject.GetFormatResult)));
            settings.error = function (XMLHttpRequest, textStatus, errorThrown) {

        function testFormat(formatter) {
            ///<summary>Method used to test formatters</summary>
            ///<param name="formatter" type="Formatting.NumberFormatter">The formatter to use</param>
            _formatter = formatter;
            var value = parseFloat(_testValue.val());


            value = formatter.TryParse(_formatted.val(), function (errorMessage, parsedValue) {


        function testParse() {
            value = _formatter.TryParse(_formatted.val(), function (errorMessage, parsedValue) {

    <form id="form1" runat="server" class="page">
    <div style="float: left">
        Name<input type="text" style="display: block" id="formatName" /></div>
    <div style="float: left">
        Value<input type="text" style="display: block" id="testValue" /></div>
        &nbsp;<input type="button" style="display: block" id="testFormat" value="Test" /></div>
    <div class="ctnt" style="clear: both">
        <div id="formatted" style="float: left">
            <input type="text" style="display:block" class="value" />
        <div id="unformatted" style="float: left; margin-left: 40px;">
            <input type="text" style="display:block" class="value" />
        <div style="float: left; margin-left: 40px;">
            <input type="button" style="display:block" id="testParse" value="Test parse" />

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Luc Veronneau
Web Developer Faltour Inc.
Canada Canada
From the beginning i have always been passionate about everything. I have never actually studied in computer programming but life brought me in IT departments from the beginning of my career. The more i programmed the more i liked it. Ever since, i've been hooked.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180515.1 | Last Updated 21 Dec 2010
Article Copyright 2010 by Luc Veronneau
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid