简体   繁体   English

将十进制值四舍五入为精确的整数值php

[英]round up decimal values to exact integer value php

I have came across the php rounding decimal values by using: 我遇到过使用以下方法对php取整十进制值:
The round() function rounds a floating-point number. round()函数舍入一个浮点数。
To round a number UP to the nearest integer, look at the ceil() function. 为了圆一个数最近的整数,看看小区()函数。
To round a number DOWN to the nearest integer, look at the floor() function. 要将数字DOWN舍入到最接近的整数,请查看floor()函数。

I have an example of dividing 1000/13 which when we use 我有一个除以1000/13的例子
round(1000/13, 2) will give you 999.98 and then i will use ceil to round it to 1000. round(1000/13,2)将给您999.98,然后我将使用ceil将其舍入到1000。
Now, my value is 925.6 which has to be divided by 11 and I tried with the 现在,我的值是925.6,必须除以11,然后尝试使用
round(925.6/11, 2) which will give 84.15, then if u multiply this with 11(84.15 * 11)then you will get 925.56 . round(925.6 / 11,2)将得到84.15,然后,如果将u乘以11(84.15 * 11),将得到925.56 Now how can achieve the exact decimal if any decimal input. 现在,如果有任何十进制输入,如何实现精确的十进制。

Rounding-off a Double or Float using round() , ceil() or floor() all have one thing in Common and that is: by using them you are subscribing to losing the Precision of your Data. 使用round()ceil()floor()对Double或Float进行round()都有一个共同点,那就是:通过使用它们,您正在失去数据的精度。 This means; 这意味着; once the die is cast, you cannot get the Original value again.... it is that simple: 铸模铸造后,您将无法再次获得原始值...。就这么简单:

If you want to retain both the Truncated and the Original Values, you have to do it differently.... You may need to keep 2 Variables or simply put your Variables in an Array. 如果要保留截断值和原始值,则必须另作不同。...您可能需要保留2个变量,或者只是将变量放入数组中。 A Simple example would demonstrate this even better: 一个简单的例子可以更好地说明这一点:

<?php

        $num        = (925.6/11);

        // STORE THE ORIGINAL & ROUNDED VALUES IN AN ARRAY (IN CASE YOU NEED BOTH)
        $arrNumbers = [
                'original'  =>  $num,
                'rounded'   =>  round($num, 2),
        ];

        // NOW; FOR YOUR MULTIPLICATIONS:
        $original   = $arrNumbers['original'] * 11;
        $broken     = $arrNumbers['rounded']  * 11;


        var_dump($original);    //<== float 925.6
        var_dump($broken);      //<== float 925.65    :: ADDED AN EXTRA 0.05 (BOGUS, HUH?)
        var_dump($arrNumbers);  //<== array (size=2) 'original' => float 84.1454545455  'rounded' => float 84.15

UPDATE: NUMERATOR AS A VALUE FROM FORM-FIELD. 更新:NUMERATOR作为来自表单字段的值。

     <?php

        // ASSUME THE VALUE OF THE NUMERATOR IS: 925.6
        $numerator      = htmlspecialchars(trim($_POST['some_field'])); 

        // AND THE DENOMINATOR IS NOW SOMETHING LIKE MONTH 11 AS IN: 11.0000
        $denominator    = 11.0000;                                           

        // NO PROBLEMS STILL...
        $num            = ($numerator/$denominator);

        // STORE THE ORIGINAL & ROUNDED VALUES IN AN ARRAY (IN CASE YOU NEED BOTH)
        $arrNumbers     = [
                'original'  =>  $num,
                'rounded'   =>  round($num, 2),
        ];

        // NOW; FOR YOUR MULTIPLICATION,
        // TO GET THE STARTING VALUES BACK DO:
        $original       = $arrNumbers['original'] * 11;
        $broken         = $arrNumbers['rounded']  * 11;


        var_dump($original);    //<== float 925.6
        var_dump($broken);      //<== float 925.65    :: ADDED AN EXTRA 0.05 (BOGUS, HUH?)
        var_dump($arrNumbers);  //<== array (size=2) 'original' => float 84.1454545455  'rounded' => float 84.15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM