Page 1 of 1
Numbers appearing as 0
Posted: Sun Jan 30, 2011 5:02 pm
by hoffa
Hi, I'm new to PHP and am using PHP 5.3.0 on Linux and am doing some math calculations. The issue I am having is when echoing the variable I'm seeing 0 instead of the correct value. What I have tried doing to get around this is to format some of the items using the number_format feature and then echo that variable, but that doesn't seem to be working when doing calculations.
Here is an example of the problem below:
The value below unformatted as shown on the screen:
1077.2670717977
Using, number_format($t, 2);The formated variable echos:
1,077.27
Now, when taking either of these values and multiplying by 29, a zero is returned instead of the expected value.
What is the best way of solving this problem? I think it is an issue with a number that doesn't end, not sure what it is called, float maybe? If I manually plug in the value 1077.27 and multiply by 29 it displays the correct answer.
Re: Numbers appearing as 0
Posted: Sun Jan 30, 2011 6:54 pm
by endyourif
You might need to look into
BC Math. More specifically the
bcmul function that will multiply two numbers for best precision.
Re: Numbers appearing as 0
Posted: Sun Jan 30, 2011 7:19 pm
by gooney0
I'm curious about how you're setting the variable. I had similar issue once when setting variable as text not actually numbers.
Example:
$x = "15";
or
$x = 15;
Most of the time it makes no difference but it did for me once upon a time.
Re: Numbers appearing as 0
Posted: Sun Jan 30, 2011 7:51 pm
by hoffa
Hi,
Thanks for the reply. I'm using no " for any of the variables. Here are some of the variables:
$datetime1 = new DateTime('1976-01-01');
$datetime2 = new DateTime("now");
$interval = $datetime1->diff($datetime2);
$totalyrs = $interval->format('%y');
$totaldays = $interval->format('%d');
$base = 100.9;
$rate = .07;
$totalyrs = $interval->format('%y');
$total = ($base*pow(1+$rate, $totalyrs)); // returns 1077.2670717977 as of
the formatted version of this below:
$totalf = number_format($total, 2); //returns 1077.27
Now, for the trouble:
$me3 =($rate/365)*$totaldays*$totalf; // returns 0
The same occurs if the non formate $total is used instead of $totalf
$me4 =($rate/365)*$totaldays*$total; // returns 0
If the value for $totalf is replaced with the value 1077 see below:
$me2 =($rate/365)*$totaldays*1077;
then, when echoing this, the following value is returned:
5.9898904109589
0 is displayed if the $total or $totalf is used instead of 1077.
Basically what this script is supposed to do is compound interest from period of time in the past into the future and then display the value.
Re: Numbers appearing as 0
Posted: Mon Jan 31, 2011 5:54 pm
by gooney0
From what I read regarding number_format:
"
If two parameters are given, number will be formatted with decimals decimals with a dot (".") in front, and a comma (",") between every group of thousands."
I would recommend doing the formatting right before the output. I suspect the number_format is adding commas which is turning you number into a string.
If you want to limit the number of decimal places I'd use either round() or ceil()
Hope this helps.
Re: Numbers appearing as 0
Posted: Mon Jan 31, 2011 7:20 pm
by hoffa
Thanks for the response. To make it clear, the issue occurs with the answer being 0 whether the number_format is used or not.