Search code examples
jquerylocalizationjavascript-globalize

Format number with jQuery Globalize


I'm trying to automatically format a number with the proper localization with Globalize. When the users inputs a numeric value I need Globalize to read it, parse it in the right format, and then output it in the field where the user has entered it.

I've tried like so:

$(document).on('change','.format-me',function(){
    var value = $(this).val();
    Globalize.culture("en-US");
    console.log(Globalize.format(value));
});

But it is not working, as the logged number looks exactly the same as the one entered by the user. Why isn't Globalize changing the format as needed?


Solution

  • I've just realized that using the jquery spinner, there's a very simple way to do what I was trying:

    $(document).on('change','.check-float',function(){
        var value = $(this).val();
        $(this).spinner( "value", value );
    });
    

    When you assign a value to the spinner like so, it automatically format the value accordingly to the culture assigned to the spinner!