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
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

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.