shadiadiph Posted March 20, 2009 Share Posted March 20, 2009 I have a form where the last known amount from the database is shown $last and a field to enter a new number $current. problem i am having is for example $last=1,251.67 $current = 1,434.83 but $diff = ($current-$last); print $diff; returns some odd numbers not the correct value of 183.16 i am also sending arrays so am using the following to insert to my database. $num_queries = count($_POST['symbol']); for($i=0;$i<$num_queries;$i++) { $last = mysql_real_escape_string($_POST['last'][$i]); $current = mysql_real_escape_string($_POST['current'][$i]); $last = ($last); $current = ($current); $diff = ($current - $last); $symbol = mysql_real_escape_string($_POST['symbol'][$i]); $sql ="UPDATE `tblpricesymbolsdetails` set `last`='$last', `current`='$current', `diff`='$diff', `dtupdated`= now() where symbol='$symbol'"; if i print $last; print $current; print $diff; it is returning 1,251.67 1,434.8303030303 00 i am confused Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted March 20, 2009 Author Share Posted March 20, 2009 don't worry about this one i have found its easier to save the two values to the database and when i call them to display preg_replace the , in the thousands with nospace then do a number format on it but it is a long way around surely there is an easier way but this is the only way I can get it working right now Quote Link to comment Share on other sites More sharing options...
zavin Posted March 21, 2009 Share Posted March 21, 2009 PHP looks at a comma as a separation of 2 items instead of marking the thousands place of a number. This should give you the answer you looking for, $last = 1251.67; $current = 1434.83; $diff = $current - $last; echo $diff; but this would give you an error. $last = 1,251.67; $current = 1,434.83; $diff = $current - $last; echo $diff; Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted March 21, 2009 Share Posted March 21, 2009 You should never store numbers with a thousands separator. The comma is merely an aid for humans (or, more accurately, some humans - others use the full stop(period) and possibly other symbols) so that they can read it more easily - it is meaningless to a computer. You should place commas when the number is being outputted for a human to read. Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted March 21, 2009 Author Share Posted March 21, 2009 thank you I will never do that again then. Are there any entities other than humans that might have a similar problem? 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.