elentz Posted June 1, 2009 Share Posted June 1, 2009 This is so simple but I can't get it to work. I have two variables: $total $mitax I want to subtract $mitax from $total to get $subtotal Here is what I have: $subtotal= $total - $mitax; I already am outputting $mitax and the $total and the amounts are right. For example $total= 1244.70 and $mitax = 11.70 So the answer should be 1233.00 But I get -10.7 What am I doing wrong? Thanks for looking Quote Link to comment https://forums.phpfreaks.com/topic/160542-i-know-this-is-a-stupid-question/ Share on other sites More sharing options...
Alex Posted June 1, 2009 Share Posted June 1, 2009 Works for me: <?php $total= 1244.70; $mitax = 11.70; $sub = $total - $mitax; echo $sub; ?> Output: 1233 Quote Link to comment https://forums.phpfreaks.com/topic/160542-i-know-this-is-a-stupid-question/#findComment-847262 Share on other sites More sharing options...
Maq Posted June 1, 2009 Share Posted June 1, 2009 Works fine for me. Post more of the surrounding code. Where do $total and $mitax come from? Quote Link to comment https://forums.phpfreaks.com/topic/160542-i-know-this-is-a-stupid-question/#findComment-847264 Share on other sites More sharing options...
roopurt18 Posted June 1, 2009 Share Posted June 1, 2009 We need to see a little bit more code and a little bit more of your program's output to help, because it sounds like it should be working. But it's not, so you've made a mistake elsewhere. Quote Link to comment https://forums.phpfreaks.com/topic/160542-i-know-this-is-a-stupid-question/#findComment-847265 Share on other sites More sharing options...
Mark Baker Posted June 1, 2009 Share Posted June 1, 2009 $total = 1244.70; $mitax = 11.70; $subtotal = $total - $mitax; echo $total.' - '.$mitax.' = '.$subtotal.'<br />'; $total = '1,244.70'; $mitax = '11.70'; $subtotal = $total - $mitax; echo $total.' - '.$mitax.' = '.$subtotal.'<br />'; gives 1244.7 - 11.7 = 1233 1,244.70 - 11.70 = -10.7 Now go ensure that your $total and $mitax values are actual numbers and not formatted strings Quote Link to comment https://forums.phpfreaks.com/topic/160542-i-know-this-is-a-stupid-question/#findComment-847306 Share on other sites More sharing options...
Ken2k7 Posted June 1, 2009 Share Posted June 1, 2009 The comma obviously is the problem. Casting the string '1,244.70' as an int would result in the number 1, or whatever the number is up to a non-numeric character, which in this case is the comma. Quote Link to comment https://forums.phpfreaks.com/topic/160542-i-know-this-is-a-stupid-question/#findComment-847309 Share on other sites More sharing options...
roopurt18 Posted June 1, 2009 Share Posted June 1, 2009 $total = '1,244.70'; PHP will convert this to a number before subtracting. '1,244' converted to a number is 1. http://www.php.net/language.types.type-juggling Quote Link to comment https://forums.phpfreaks.com/topic/160542-i-know-this-is-a-stupid-question/#findComment-847311 Share on other sites More sharing options...
elentz Posted June 2, 2009 Author Share Posted June 2, 2009 Well, Mark had the right idea, I had BOTh of those numbers formatted. Once I removed that it woked out. But at some point I need to format the numbers. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/160542-i-know-this-is-a-stupid-question/#findComment-847454 Share on other sites More sharing options...
roopurt18 Posted June 2, 2009 Share Posted June 2, 2009 Well, Mark had the right idea, I had BOTh of those numbers formatted. Once I removed that it woked out. But at some point I need to format the numbers. Format it after all calculations are made or just before display. Quote Link to comment https://forums.phpfreaks.com/topic/160542-i-know-this-is-a-stupid-question/#findComment-847477 Share on other sites More sharing options...
elentz Posted June 2, 2009 Author Share Posted June 2, 2009 That is exactly what I did and it works perfectly, Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/160542-i-know-this-is-a-stupid-question/#findComment-847483 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.