Jump to content

having problems with numbers


shadiadiph

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/150372-having-problems-with-numbers/
Share on other sites

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

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;

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.