/ Published in: JavaScript
Used to easily display or compute for prices e.g shopping cart.
Thousands separator regex based on Paul Creasey.
Thousands separator regex based on Paul Creasey.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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; } }; });