dsirr48088 2014-09-29 13:00
浏览 50

PHP Division Float Value WorkAround

Kindly refer: PHP Division Float Value Issue

With reference to the Question I have wrote the following code. Can this be done in different way or less code than this.?

The following will help when any of the two given value are lesser than 1 with decimal values. Is this a good practice?

<?php

        $First_Value = 0.005;
        $Second_Value = 0.1;

        // Check the First Value for Decimal
        $First_Whole = floor($First_Value);  // 0
        $First_Fraction = $First_Value - $First_Whole; // .005

        // Check the Second Value for Decimal
        $Second_Whole = floor($Second_Value); // 0
        $Second_Fraction = $Second_Value - $Second_Whole; // .1

        // Multiply with 100 becaus ethe decimal is restricted to 3 digit so multiply with 100 works
        if(($First_Whole == 0) || ($Second_Whole == 0)){
            $First_Value = $First_Value*100; // 0.5
            $Second_Value = $Second_Value*100; // 10
        }

        echo $tableCount = $Second_Value / $First_Value; // 20
?>
  • 写回答

1条回答 默认 最新

  • dongwo7858 2014-09-29 13:05
    关注

    You don't need to do any of that calculation. just do the last division.

    ...
    $value1 = .005;
    $value2 = .1;
    
    echo $value1 / $value2;
    

    results in 20

    If you are trying to get just 2 decimals you can use number format after dividing. If you are trying to format before dividing then you should still be able to number format. If however, you cannot then why not just multiply both numbers by 1000 (three decimals) % by 1 and minus the difference of the mod from the actual. that will give you 3 in this case, divide back by 1000

    [EDIT]

    to get the remainder do the following example

    ...
    $v = .0055;
    $d = .1;
    
    $a = $d/$v;
    $s = $a % ceil($a);
    $r = $a - $s;
    

    answer = .18181818181818181...

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。