matfish Posted August 31, 2010 Share Posted August 31, 2010 Hi there, I have a shopping basket which I'm trying to add up in JS depending on which delivery method the customer chooses, however I have values such as 1,200 + 25.00 but I keep getting "NaN" when trying to add them I have a feeling it's the larger values - any help? Link to comment https://forums.phpfreaks.com/topic/212224-number-formatting-currency/ Share on other sites More sharing options...
brianlange Posted September 1, 2010 Share Posted September 1, 2010 replace the comma with an empty string before doing the addition. var amount1 = '1,200'; var amount2 = 25.00; var total = amount1.replace(',', '') + amount2; Link to comment https://forums.phpfreaks.com/topic/212224-number-formatting-currency/#findComment-1105879 Share on other sites More sharing options...
Alex Posted September 1, 2010 Share Posted September 1, 2010 You'll also have to convert the string to an integer using parseInt(): var amount1 = '1,200'; var amount2 = 25.00; var total = parseInt(amount1.replace(',', '')) + amount2; Link to comment https://forums.phpfreaks.com/topic/212224-number-formatting-currency/#findComment-1105887 Share on other sites More sharing options...
matfish Posted September 1, 2010 Author Share Posted September 1, 2010 Brilliant thank you. Removing the comma in the string, converting it to a number then adding the amounts together worked a treat. However, how do I go about number formatting this back to x,xxx.xx like UK currency? Many thanks for your help. Link to comment https://forums.phpfreaks.com/topic/212224-number-formatting-currency/#findComment-1106041 Share on other sites More sharing options...
Alex Posted September 1, 2010 Share Posted September 1, 2010 http://phpjs.org/functions/number_format:481 Link to comment https://forums.phpfreaks.com/topic/212224-number-formatting-currency/#findComment-1106128 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.