[英]Round up if negative round down if positive php?
Round up if negative round down if positive? 如果否定则向上舍入如果正则向下舍入? I have
我有
$rounded =1000
39528,65 round should be --> 39000
AND 和
-30965,77 --> -31000
Have a try with : 尝试一下:
$rounded = 1000;
foreach(array(39528.65, -30965.77) as $num) {
echo $num,' -> ', floor($num/$rounded)*$rounded,"\n";
}
output: 输出:
39528.65 -> 39000
-30965.77 -> -31000
A direct int cast seems do this job, if your rounding up/down means floor/ceil 如果您的向上/向下取整表示下限/上限,则直接进行int强制转换似乎可以完成此任务
echo (int)( 8.5/8); // 1 = floor( 8.5/8)
echo (int)(-8.5/8); // -1 = ceil(-8.5/8)
You can use abs to get the absolute value, like so: 您可以使用abs来获取绝对值,如下所示:
round(abs(-30965,77))
If your number is a negative, abs
will return a positive. 如果您的数字为负数,则
abs
将返回正数。 Otherwise it will return the positive number. 否则它将返回正数。 You can then use
round
(with your desired precision) to round your value. 然后,您可以使用
round
(以所需的精度)舍入您的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.