简体   繁体   English

php轮到下一个整数

[英]php round to the next whole number

In PHP, I need to round off a number to the next whole number. 在PHP中,我需要将一个数字四舍五入到下一个整数。 For example, in my program, I am using the round as below. 例如,在我的程序中,我使用如下的回合。

$val = round(count($names)/15);

If count($names) is 1.2, I need it to be rounded off to 2 instead of 1. I tried to do the below approach. 如果count($ names)是1.2,我需要将它四舍五入为2而不是1.我试着做下面的方法。

  $val = round(count($names)/15) + 1;

However, in the above approach, if I have count($names) as 1.6, It is getting rounded off to 3 as I increment it by 1. 但是,在上述方法中,如果我将count($ names)设为1.6,则当我将其递增1时,它会四舍五入为3。

Is there a way where no matter what the decimal value is, it needs to be rounded off to the next whole number? 有没有办法无论小数值是多少,都需要四舍五入到下一个整数?

How about using ceil() 如何使用ceil()

http://php.net/manual/en/function.ceil.php http://php.net/manual/en/function.ceil.php

Then your code becomes: 然后你的代码变成:

$val = ceil(count($names)/15);

尝试这个:

$val = round(count($names)/15, PHP_ROUND_HALF_DOWN) + 1;

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

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