NerdConcepts Posted September 5, 2007 Share Posted September 5, 2007 I cannot add say 1,000 and 1,000 $total = '1,000' + '1,000'; when you echo $total it comes out '2' yet if I do $total = '1000' + '1000'; it echos out '2000' like it should, one problem. All my pricing values have commas in them. Almost all if not all will always contain a comma and NOT a period. Just wondering how in the check can I remove the comma, add the values, then replace it? BTW, it's for a profit/loss chart. It scans a list a purchased and sold vehicles and does some math and spits out totals. Values are stored as a VARCHAR(20) in the database. Not sure what needs to be done, not sure if storing the numbers different would fix the problem or what. Link to comment https://forums.phpfreaks.com/topic/68009-stupid-math-problem/ Share on other sites More sharing options...
SJames Posted September 5, 2007 Share Posted September 5, 2007 The problem is that 1,000 is a string, not a number. Removing the comma will make it a number. The following should work: $number1 = "1,000"; $number2 = "1,000"; $number1 = str_replace(",", "", $number1); $number2 = str_replace(",", "", $number2); $total = $number1 + $number2; $total = number_format($total); Now when you echo $total, it comes out to 2,000 Link to comment https://forums.phpfreaks.com/topic/68009-stupid-math-problem/#findComment-341889 Share on other sites More sharing options...
gerkintrigg Posted September 5, 2007 Share Posted September 5, 2007 or just: $number1=1000; $number2=1000; $total=number_format($number1+$number2); echo $total; Link to comment https://forums.phpfreaks.com/topic/68009-stupid-math-problem/#findComment-341922 Share on other sites More sharing options...
NerdConcepts Posted September 5, 2007 Author Share Posted September 5, 2007 That is easy, how come I always run into problems on the easy things, lol. Link to comment https://forums.phpfreaks.com/topic/68009-stupid-math-problem/#findComment-341925 Share on other sites More sharing options...
bmdsherman Posted July 4, 2009 Share Posted July 4, 2009 Your original script was basically saying: $total = 1 + 000 + 1 + 000 Link to comment https://forums.phpfreaks.com/topic/68009-stupid-math-problem/#findComment-868653 Share on other sites More sharing options...
Psycho Posted July 4, 2009 Share Posted July 4, 2009 If these values are coming from a database you shoudl really consider saving the values as nubers and not strings to begin with. You can always format the number when printing to a page. Link to comment https://forums.phpfreaks.com/topic/68009-stupid-math-problem/#findComment-868664 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.