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? Link to comment https://forums.phpfreaks.com/topic/135363-double-numbers-not-working-right/ 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> Link to comment https://forums.phpfreaks.com/topic/135363-double-numbers-not-working-right/#findComment-705054 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 Link to comment https://forums.phpfreaks.com/topic/135363-double-numbers-not-working-right/#findComment-705077 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 Link to comment https://forums.phpfreaks.com/topic/135363-double-numbers-not-working-right/#findComment-705084 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. Link to comment https://forums.phpfreaks.com/topic/135363-double-numbers-not-working-right/#findComment-705322 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 Link to comment https://forums.phpfreaks.com/topic/135363-double-numbers-not-working-right/#findComment-705336 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 Link to comment https://forums.phpfreaks.com/topic/135363-double-numbers-not-working-right/#findComment-705791 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 Link to comment https://forums.phpfreaks.com/topic/135363-double-numbers-not-working-right/#findComment-705793 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> Link to comment https://forums.phpfreaks.com/topic/135363-double-numbers-not-working-right/#findComment-705932 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.