在 PHP 中向下舍入到最接近的半整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3216482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:02:24  来源:igfitidea点击:

Round DOWN to nearest half integer in PHP

php

提问by Chris Fletcher

I need a PHP function that will take a float and round it down to the nearest half(x.0 or x.5). I found other functions that will round to the nearest fraction, but they round both ways.

我需要一个 PHP 函数,它将采用一个浮点数并将其四舍五入到最接近的一半(x.0 或 x.5)。我发现其他函数会四舍五入到最接近的分数,但它们都是双向的。

The function I need can only round down.

我需要的功能只能四舍五入。

Examples

例子

7.778 -> 7.5

7.778 -> 7.5

7.501 -> 7.5

7.501 -> 7.5

7.49 -> 7.0

7.49 -> 7.0

7.1 -> 7.0

7.1 -> 7.0

回答by Dominic Rodger

$x = floor($x * 2) / 2;

回答by tzaman

I'm assuming PHP has a floor function: floor($num * 2) / 2ought to do it.

我假设 PHP 有一个地板功能:floor($num * 2) / 2应该这样做。

回答by Berserk

A easy solution is to use modulo operator (fmod()function), like this :

一个简单的解决方案是使用模运算符(fmod()函数),如下所示:

function roundDown($number, $nearest){
    return $number - fmod($number, $nearest);
}

var_dump(roundDown(7.778, 0.5));
var_dump(roundDown(7.501, 0.5));
var_dump(roundDown(7.49, 0.5));
var_dump(roundDown(7.1, 0.5));

And the result :

结果:

enter image description here

在此处输入图片说明

The advantage it's that work with any nearest number (0.75, 22.5, 3.14 ...)

它的优点是可以使用任何最接近的数字(0.75、22.5、3.14 ...)

You can use the same operator to roundUp :

您可以使用相同的运算符来舍入:

function roundUp($number, $nearest){
    return $number + ($nearest - fmod($number, $nearest));
}

var_dump(roundUp(7.778, 0.5));
var_dump(roundUp(7.501, 0.5));
var_dump(roundUp(7.49, 0.5));
var_dump(roundUp(7.1, 0.5));

enter image description here

在此处输入图片说明

回答by xavadu

You can do it on that way round($number / 5, 1) * 5the second parameter in the round()is the precision.

你可以这样做,round($number / 5, 1) * 5第二个参数round()是精度。

Example with $numberequal to 4.6, 4.8 and 4.75

$number等于 4.6、4.8 和 4.75 的示例

>>> round(4.6 / 5, 1) * 5;
=> 4.5
>>> round(4.8 / 5, 1) * 5;
=> 5.0
>>> round(4.75 / 5, 1) * 5;
=> 5.0

If you want you can round()down too like round($number, 1, PHP_ROUND_HALF_DOWN)check the documentation for more information https://www.php.net/manual/en/function.round.php

如果你愿意,你可以round()倒也像round($number, 1, PHP_ROUND_HALF_DOWN)检查的详细信息的文档https://www.php.net/manual/en/function.round.php

回答by Kiran

echo round($val*2) / 2;    // Done

回答by chuyen nha

From my job's requirements. I put an function to do this. Hope you can view it as a reference:

从我的工作要求来看。我放了一个函数来做到这一点。希望大家可以作为参考:

function round_half_five($no) {

    $no = strval($no);
    $no = explode('.', $no);
    $decimal = floatval('0.'.substr($no[1],0,2)); // cut only 2 number
    if($decimal > 0) {
        if($decimal <= 0.5) {
            return floatval($no[0]) + 0.5;
        } elseif($decimal > 0.5 && $decimal <= 0.99) {
            return floatval($no[0]) + 1;
        }
    } else {
        return floatval($no);
    }

}