php 四舍五入到最接近的分数(一半、四分之一等)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14903379/
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
Rounding to nearest fraction (half, quarter, etc.)
提问by Wiseguy
So, I need to create the following functions but my head can't think of any possibility in PHP without complicated math.
所以,我需要创建以下函数,但如果没有复杂的数学,我的头脑无法想到 PHP 中的任何可能性。
- Round alwaysup to the nearest decimal (1.81 = 1.90, 1.89 = 1.90, 1.85 = 1.90)
- Round alwaysdown to the nearest decimal (1.81 = 1.80, 1.89 = 1.80, 1.85 = 1.80)
- Round alwaysup to the nearest x.25 / x.50 / x.75 / x.00 (1.81 = 2, 1.32 = 1.50)
- Round alwaysdown to the nearest x.25 / x.50 / x.75 / x.00 (1.81 = 1.75, 1.32 = 1.25)
- Round alwaysup to the nearest x.50 / 1 (1.23 = 1.50, 1.83 = 2)
- Round alwaysdown to the nearest x.50 / 1 (1.23 = 1, 1.83 = 1.50)
- 轮始终到最接近的十进制(1.81 = 1.90,1.89 1.90 = 1.85 1.90 =)
- 轮始终至最接近的十进制(1.81 = 1.80,1.89 = 1.80Hz,1.85 = 1.80)
- 轮始终到最接近的X.25 / X.50 / X.75 /×.00(1.81 = 2,1.32 = 1.50)
- 轮始终至最接近的X.25 / X.50 / X.75 /×.00(1.81 = 1.75,1.32 = 1.25)
- 轮始终到最接近的X.50 / 1(1.23 = 1.50,1.83 = 2)
- 轮始终至最接近的X.50 / 1(1.23 = 1,1.83 = 1.50)
I have searched on Google for 2 hours now and the only things that came up were Excel forums. Is it possible with some simple lines of PHP?
我已经在谷歌上搜索了 2 个小时,唯一出现的就是 Excel 论坛。是否可以使用一些简单的 PHP 行?
回答by Wiseguy
Since you're looking for fourths(.00, .25, .50, .75), multiply your number by 4, round to nearest whole number as desired (floorif down, ceilif up), then divide by 4.
由于您正在寻找四分之三( .00, .25, .50, .75),将您的数字乘以 4,根据需要舍入到最接近的整数(floor如果向下,ceil如果向上),然后除以 4。
1.32, down to nearest fourth:
1.32,下降到最接近的第四个:
1.32 * 4 = 5.28
floor(5.28) = 5.00
5.00 / 4 = 1.25
1.32 * 4 = 5.28
楼层(5.28) = 5.00
5.00 / 4 = 1.25
Same principle applies for any other fractions, such as thirdsor eighths(.0, .125, .25, .375, .5, .625, .75, .875). For example:
相同的原则适用于任何其他分数,例如三分或八分( .0, .125, .25, .375, .5, .625, .75, .875)。例如:
1.77, up to nearest eighth:
1.77,到最接近的八分之一:
1.77 * 8 = 14.16
ceil(14.16) = 15.00
15.00 / 8 = 1.875
1.77 * 8 = 14.16
ceil(14.16) = 15.00
15.00 / 8 = 1.875
Just for fun, you could write a function like this:
只是为了好玩,你可以写一个这样的函数:
function floorToFraction($number, $denominator = 1)
{
    $x = $number * $denominator;
    $x = floor($x);
    $x = $x / $denominator;
    return $x;
}
echo floorToFraction(1.82);      // 1
echo floorToFraction(1.82, 2);   // 1.5
echo floorToFraction(1.82, 3);   // 1.6666666666667
echo floorToFraction(1.82, 4);   // 1.75
echo floorToFraction(1.82, 9);   // 1.7777777777778
echo floorToFraction(1.82, 25);  // 1.8
回答by Bart Guliker
Please note that the answer isn't really water tight. Since we're dealing with floats here it's not guaranteed that when you divide the rounded number by the denominator it returns a neatly round number. It may return 1.499999999999 instead of 1.5. It's the nature of floating point numbers.
请注意,答案并不是真的水密。由于我们在这里处理浮点数,因此不能保证当您将四舍五入的数字除以分母时,它会返回一个整齐的四舍五入数字。它可能会返回 1.499999999999 而不是 1.5。这是浮点数的本质。
Another round is needed before returning the number from the function.
在从函数返回数字之前需要另一轮。
Just in case someone lands here from google like I did :)
以防万一有人像我一样从谷歌登陆这里:)
回答by Aryeh Beitz
According to the mround()function in Excel:
根据mround()Excel中的函数:
function MRound($num,$parts) {
    $res = $num * $parts;
    $res = round($res);
    return $res /$parts;
}
echo MRound(-1.38,4);//gives -1.5
echo MRound(-1.37,4);//gives -1.25
echo MRound(1.38,4);//gives 1.5
echo MRound(1.37,4);//gives 1.25
回答by Catfish
Look at example #3 on here and it is half of your solution - http://php.net/manual/en/function.round.php
看看这里的示例 #3,它是您解决方案的一半 - http://php.net/manual/en/function.round.php

