División de variables en el redondeo de PHP

I have a simple calculator script for making calculations of facebook insights.

There is a function in my script called fb_growth where I pass two arguments in: one for the current month's data (\$moc) and one for the previous month's data (\$mop) and I am trying to find the growth percentage (positive or negative) between the 2. Here is my function script:

//Growth Calculator Function
function fb_growth(\$moc, \$mop) {
if(\$moc>=\$mop) {
\$grp = (\$moc/\$mop);
\$grf = (\$grp - 1);
return \$grf;
}
else if(\$moc<\$mop) {
\$grp = (\$mop/\$moc);
\$grf = (1 - \$grp);
return \$grf;
}
}

The values I enter in are coming from a form that the user fills out. In this particular case, the numbers passed in are fb_growth(\$fbfi1, \$fbfi2); where \$fbfi1 equals the string '1719223' and \$fbfi2 equals the string '1859867'. In this case, \$moc is less than \$mop, so 1719223 gets divided by 1859867, which should return 1.08180672315, but instead it returns 1, which then gets subtracted by 1 to get 0. What I want to end up with is '-.08180672315', which would be my growth percentage, but I cannot get it to give me this outcome. If I echo ("1859867"/ "1719223"); then I get 1.08180672315, but when the strings are held in the variables and I echo ("\$fbfi2" / "\$fbfi1"); then I get 1.

I tried settype() to a double and an int for \$grf and for both \$fbfi1 & 2 and same result. If I set \$grf as a global and echo gettype(), it will give me the double or the int, but I still get the same value: 0. Is there something I can adjust in my php settings for this, or is there something wrong with my setup? I can't find any documentation. Help is MUCH appreciated, thank you!!

preguntado el 10 de marzo de 12 a las 12:03

2 Respuestas

PHP does automatic casting, see the type juggling manual. Integer division yields integer result (hence the 1).

Cast the values before doing arithmetic calculations: (float) \$var

respondido 10 mar '12, 12:03

Okay so I have casted \$fbfi1 and 2 floats with (float) \$fbfi1 = \$_POST['pvm1']; where it is setting the value from the form, as well as casting \$grp and \$grf as floats in the function, but I am still getting the same result. - Dante Cullari

My code now looks like this //Growth Calculator Function function fb_growth(\$moc, \$mop) { if(\$moc>=\$mop) { (float) \$grp = (\$moc/\$mop); (float) \$grf = (\$grp - 1); return \$grf; } elseif(\$moc<\$mop) { (float) \$grp = (\$mop/\$moc); (float) \$grf = (1 - \$grp); return \$grf; } } with the code from the above comment also for the arguments passed in - Dante Cullari

you're still doing integer divison. one of the operands must be float. - karoly horvath

And the problem was... comma's in the entry numbers!! I was inputing 123,456 instead of 123456. That's what you get when you just carelessly copy and paste sometimes..wow. Thanks for all the help though, I learned a lot about casting!

Respondido el 06 de junio de 12 a las 15:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.