Return to Snippet

Revision: 37743
at December 16, 2010 14:30 by Arvi


Initial Code
jQuery(document).ready(function($) {

   $.currency = {

       displayFormat: function(amount)
       {
           amount = $.currency.convert_to_decimal(amount);
           amount = $.currency.add_thousands_separator(amount);
           
           return amount;
       },

       computeFormat: function(amount)
       {
           amount = $.currency.remove_thousands_separator(amount);
           amount = $.currency.convert_to_decimal(amount);

           return amount;
       },

       add_thousands_separator: function(amount)
       {
            amount = String(amount);
            amount = amount.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");

            return amount;
       },

       remove_thousands_separator: function(amount)
       {
           amount = String(amount);
           amount = amount.replace(/\,/g, "");

           return amount;
       },

       convert_to_decimal: function(amount)
       {
           amount = Number(amount).toFixed(2);

           return amount;
       }
   };
});

Initial URL

                                

Initial Description
Used to easily display or compute for prices e.g shopping cart.

Thousands separator regex based on Paul Creasey.

Initial Title
Javascript - Manipulating currency for display and computation

Initial Tags

                                

Initial Language
JavaScript