dongxie3681 2012-03-10 12:23
浏览 40
已采纳

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

  • 写回答

2条回答 默认 最新

  • duanhao9176 2012-06-06 14:11
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看