Page 1 of 1

Equation breakdown

Posted: Mon Mar 05, 2012 1:35 pm
by bukwus
Hi

I have built a series of forms with PHP and have been using SESSIONS to carry values from one to another. In one, the value of a variable is determined by adding the values of several other variables together:

Code: Select all

$totalFixed = $fixedCost1 + $fixedCost2 + $fixedCost3 + $fixedCost4 + $fixedCost5;
This equation is working fine on that page. I then place the value of $totalFixed in a SESSION variable:

Code: Select all

$_SESSION['totalFix']=$totalFixed;
I then assign this SESSION variable's value to a new variable on another page in the series:

Code: Select all

$dataValue[4] = $_SESSION['totalFix'];
The number displays fine on this page as well. However, when the value is above 1,000 and I try to subtract another number from it, it ignores everything to the right of the comma. For example, if $dataValue[4] is 10,132 and $dataValue[5] is 25 and I subtract $dataValue[4] from $dataValue[5], the result is -15.
Does anyone know the reason for this?
Many thanks,
Andy

Re: Equation breakdown

Posted: Mon Mar 05, 2012 1:55 pm
by Celauran
Because 10,132 is a string. You want 10132.

Re: Equation breakdown

Posted: Mon Mar 05, 2012 2:22 pm
by califdon
Expanding a bit on Celauran's response, we humans like to put commas into our numbers (Europeans use periods) to make them easier to read, but the comma is NOT part of the number. If you have formatted a number to be displayed with a "thousands-separator", you must restore it to a pure number before you can do any calculations with it. Perhaps you have used number_format() on your value at some point. You should store numbers as pure numbers to avoid this problem, formatting them only to display them, if you wish.