Reputation: 1
I have a simple question about variable type in php. I have two values in my array:
$row['DS'] // type :float (with one decimal like 12.2)
$row['TC'] // type :float (with one decimal like 24.2)
What I'm actually try to do in the make the calculation below:
$row['TC'] / $row['DS'] // $row['DS'] need to be as integer (without point,like 12)
and the result should be with two decimal like (2.32). I tried to do it in that way
$DSF = number_format($row['DS'],0);
$ConF = $row['TC'] / $DSF ;
echo number_format($conF,2);
but it returns the wrong result. for example :
$row['DS'] = 59,009.3 ---> after change the format is change to 59,009
$row['TC'] = 190.0
$ConF = 190.0 / 59,009
it should be 000.223 (something around this number ) and i expect to get 0 (after i change the format using number_format($conF,2)
but instead of this the program return me the number 3.22
What am I doing wrong?
Upvotes: 0
Views: 67
Reputation: 4114
You can use type casting
to convert $row['DS']
to integer
right in your calculations, for e.g.:
$row['TC'] / (int)$row['DS']
or
$row['TC'] / intval($row['DS'])
Upvotes: 0
Reputation: 33542
The function number_format()
is used to format numbers to a comma style representation, not to actually round numbers to what you want.
The function you are looking for is round which returns a float to a specified number of decimal places.
For example:
$yourVar=round($row['TC']/$row['DS'],2);
This means that $yourVar
will be the value of the division rounded to two decimal places.
You should use the number_format()
function only to display human-friendly numbers at the end.
Upvotes: 1