PHP部门浮动价值评估 [英] PHP Division Float Value WorkAround

查看:77
本文介绍了PHP部门浮动价值评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参考: PHP部门的浮动价值问题

关于该问题,我编写了以下代码.可以通过其他方式或更少的代码来完成此操作吗??

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

当两个给定值中的任何一个小于十进制值 1 时,以下内容将有所帮助.这是一个好习惯吗?

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
?>

推荐答案

您无需进行任何计算.只是做最后的分裂.

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

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

echo $value1 / $value2;

得出20

如果您尝试仅获取2位小数,则可以在除后使用数字格式.如果您尝试在除法之前格式化,那么您仍然应该可以对格式进行编号.但是,如果不能,那么为什么不将两个数字都乘以1000(三位小数)%乘以1,然后减去mod与实际值的差即可.在这种情况下,您将得到3,再除以1000

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

要获取余数,请执行以下示例

to get the remainder do the following example

...
$v = .0055;
$d = .1;

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

answer = .18181818181818181 ...

answer = .18181818181818181...

这篇关于PHP部门浮动价值评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆