formatting numbers i18n

if you are writing feature rich user interfaces in AJAX, where the user is asked to type in numbers, it is a good idea to use i18n to give people a natural feeling. a lot of countries use different floating point or thousands separation characters than the english speaking countries. for example in germany we use a comma for the floating point and a single point for thousands separation. handgestrickt wrote 2 handy functions to convert numbers from handwritten or i18n to a real floating point number and vice versa. feel free to use them.

var foo = {
        clean_number: function(number,decimals,dec_point,thousands_sep) {
                var negative = '';
                if(number.substr(0,1) == '-') {
                        negative = '-';
                        number = number.substr(1);
                }
                var dec_point_index = number.lastIndexOf(dec_point);
                var num_split;
                if(dec_point_index != -1) num_split = [number.substr(0,dec_point_index),number.substr((dec_point_index+1),decimals)];
                else num_split = [number];
                num_split[0] = this.replace_from_string(num_split[0],thousands_sep,'');
                if(num_split[1]) number = num_split[0]+'.'+num_split[1];
                else number = num_split[0];
                number = negative+number.replace(/[^0-9\\.]+/g,'');
                return parseFloat(number);
        },

        number_format: function(number,decimals,dec_point,thousands_sep) {
                number = String(number);
                var negative = '';
                if(number.substr(0,1) == '-') {
                        negative = '-';
                        number = number.substr(1);
                }
                var dec_point_index = number.lastIndexOf('.');
                var num_split;
                if(dec_point_index != -1) {
                        num_split = [number.substr(0,dec_point_index),number.substr((dec_point_index+1),decimals)];
                        if(num_split[1].search(/^0+$/) != -1) num_split[1] = null;
                }
                else num_split = [number];
                var new_number = '';
                for(var a=0;a<Math.ceil(num_split[0].length/3);a++) {
                        new_number = num_split[0].substring(
                                (num_split[0].length-(a*3)-3),
                                (num_split[0].length-(a*3))
                        )+
                        thousands_sep+
                        new_number;
                }
                new_number = new_number.substring(0,(new_number.length-1));
                if(num_split[1]) number = new_number+dec_point+num_split[1];
                else number = new_number;
                return negative+number;
        },
        
        replace_from_string: function(string,placeholder,replacement) {
                eval("string = string.replace(/"+this.regexp_quote(placeholder)+"/g,replacement);");
                return string;
        },

        regexp_quote: function(string) {
                return string.replace(/(\|\/|\||\+|\*|\?|\(|\)|\[|\]|\{|\}|\^|\.|\"|\'|$|\r|\n|\t|\f|\@)/g,"\$1");
        }
}

Thursday, 22. March 2007 • trackback url

Add Comment

( to reply to a comment, click the reply link next to the comment )

Comment Title:
Your Name:
Email Address:
Make Public?
Website:
Make Public?

Comment:


Allowed XHTML tags : a, b, i, strong, code, acrynom, blockquote, abbr. Linebreaks will be converted automatically.


Captcha:

captcha image

please type the content of the above image into the following form-field: