Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

JavaScript Number Parsing and Formatting for Multicultural Environments

, 21 Dec 2010 CPOL
A JavaScript class used to help in formatting and parsing numbers when parseInt and parseFloat are not enough
var arrFormatted = [];
var arrUnformatted = [];

var results = {};


var eventManager = {
    listeners: [],
    RegisterListener: function (listenerObject) {
        ///<summary>Registers a listener object</summary>
        ///<param name="listenerObject" type="eventManager.IListener" />

        this.listeners.push(new eventManager.IListener(listenerObject));
    },
    IListener: function () {
        if (arguments.length > 0
            && typeof arguments[0]["NewMessage"] === "function")
            return arguments[0];
        throw ("Object does not implement IListener");
    },
    SendMessage: function (message) {
        for (var itm in this.listeners)
            this.listeners[itm].NewMessage(message);
    }
}

eventManager.IListener.prototype = {
    NewMessage: function (message) {
        ///<summary>Receives new message</summary>
        ///<param name="message" type="String" />
    }
};

function LaunchTests() {
    setTimeout(function () {
        eventManager.SendMessage("Initializing test....");
        for (var i = 0; i < 300000; i++) {
            arrFormatted.push((Math.round((Math.random() * 1000)) / 100).toString());
        }
        for (var i in arrFormatted)
            arrUnformatted.push(arrFormatted[i].replace(".", ""));
        eventManager.SendMessage("Starting tests for eval");
        setTimeout(function () {
            var d = new Date();
            for (var i in arrFormatted)
                eval(arrFormatted[i]);
            var result = new Date((new Date()) - d);
            eventManager.SendMessage("Eval test result : " + result.getMinutes() + ":" + result.getSeconds());
            eventManager.SendMessage("Starting tests for parseInt");
            setTimeout(function () {
                var d = new Date();
                for (var i in arrUnformatted)
                    parseInt(arrUnformatted[i]) / Math.pow(10, 2);
                var result = new Date((new Date()) - d);
                eventManager.SendMessage("parseInt test result : " + result.getMinutes() + ":" + result.getSeconds());
                eventManager.SendMessage("Starting tests for new Number");
                setTimeout(function () {
                    var d = new Date();
                    for (var i in arrFormatted)
                        new Number(arrFormatted[i]);
                    var result = new Date((new Date()) - d);
                    eventManager.SendMessage("new Number test result : " + result.getMinutes() + ":" + result.getSeconds());
                }, 1);
            }, 1);
        }, 1);
    }, 1);
}

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.

License

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

Share

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 21 Dec 2010
Article Copyright 2010 by Luc Veronneau
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid