glenelkins Posted December 3, 2008 Share Posted December 3, 2008 Hi I have 2 vars: var price = 0.50; var delivery = 10.00; if i do: price = price + delivery; the output of price comes at : 0.5010 when it should be 10.50 any ideas? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 3, 2008 Share Posted December 3, 2008 i get 10.5 <script type="text/javascript"> var price = 0.50; var delivery = 10.00; price = price + delivery; alert(price); </script> how are you getting these numbers? from text fields or something? cus then they would be strings and javascript will use concatenation. make sure they are float numbers with parseFloat(): <script type="text/javascript"> var price = document.getElementById('price').value; var delivery = document.getElementById('delivery').value; price = parseFloat(price) + parseFloat(delivery); alert(price); </script> Quote Link to comment Share on other sites More sharing options...
glenelkins Posted December 3, 2008 Author Share Posted December 3, 2008 hi ok that worked to an extent. But when its multiplying 0.50 by 10.00 it comes out as 10.5 i want it to say 10.50 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 3, 2008 Share Posted December 3, 2008 price.toFixed(2); will format it to have 2 decimals Quote Link to comment Share on other sites More sharing options...
gathos Posted December 3, 2008 Share Posted December 3, 2008 hehe i just posted something similar, what is happening is that your variables are strings, so it's adding two strings together, not numbers. a way that i was to to fix it was price = +price + +delivery; it turns them from a string into a integer. i hope this helped. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 3, 2008 Share Posted December 3, 2008 hehe i just posted something similar, what is happening is that your variables are strings, so it's adding two strings together, not numbers. a way that i was to to fix it was price = +price + +delivery; it turns them from a string into a integer. i hope this helped. interesting...i've never seen a double plus used like that before. i always use parseInt(). but also, the OP needs FLOATs, not INTs Quote Link to comment Share on other sites More sharing options...
glenelkins Posted December 4, 2008 Author Share Posted December 4, 2008 gathos... thats cool, but im using doubles ( floating point ) not integers but thanks anyway Quote Link to comment Share on other sites More sharing options...
glenelkins Posted December 4, 2008 Author Share Posted December 4, 2008 price = parseFloat ( price ) - parseFloat ( delivery_cost ); price.toFixed(2); price is stil coming out as a 1 decimal place Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 4, 2008 Share Posted December 4, 2008 i get 2 decimals with the following: <script> var price = '0.50'; var delivery_cost = '10.00'; price = parseFloat(price) + parseFloat(delivery_cost); price = price.toFixed(2); alert(price); </script> 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.