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? Quote Link to comment 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; Quote Link to comment 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; Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Alex Posted September 1, 2010 Share Posted September 1, 2010 http://phpjs.org/functions/number_format:481 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.