百科狗-知识改变命运!
--

ceil() - 进一法取整 - php 数学函数

乐乐1年前 (2023-11-21)阅读数 27#技术干货
文章标签整数

ceil()

(PHP 4, PHP 5, PHP 7)

进一法取整

说明

ceil(float $value):float

返回不小于$value的下一个整数,$value如果有小数部分则进一位。

参数

$value

要进一法取整的值

返回值

返回不小于$value的下一个整数。ceil()返回的类型仍然是float,因为float值的范围通常比integer要大。

范例

ceil() - 进一法取整 - php 数学函数

Example #1ceil()例子

参见

  • floor() 舍去法取整
  • round() 对浮点数进行四舍五入
I needed this and couldn't find it so I thought someone else wouldn't have to look through a bunch of Google results- 
Actual behaviour:
echo ceil(-0.1); //result "-0" but i expect "0"
Workaround:
echo ceil(-0.1)+0; //result "0"
Ceil for decimal numbers with precision:
function ceil_dec($number,$precision,$separator)
{
  $numberpart=explode($separator,$number); 
$numberpart[1]=substr_replace($numberpart[1],$separator,$precision,0);
  if($numberpart[0]>=0)
  {$numberpart[1]=ceil($numberpart[1]);}
  else
  {$numberpart[1]=floor($numberpart[1]);}
  $ceil_number= array($numberpart[0],$numberpart[1]);
  return implode($separator,$ceil_number);
}
echo ceil_dec(1.125,2,"."); //1.13
echo ceil_dec(-1.3436,3,"."); //-1.343
echo ceil_dec(102938.1,4,"."); //102938.1
Or for the terniary fans:

Slightly pointless, but there you have it, in one line only..
Please see http://www.php.net/manual/en/language.types.float.php for information regarding floating point precision issues.
$test = (1 - 0.7) * 10;
$test_1 = ceil($test);
$test_2 = ceil($test * 10);
var_dump($test_1);  // float 4
var_dump($test_2);  // float 31
Caution!

See round() doc for more information on this.
Here is a navbar using the ceil function. 
float ceil
function fCeil($val,$pressision=2){
   $p = pow(10,$pressision);
  $val = $val*$p;
  $val = ceil($val);
 return $val /$p;
}
Note that 'ceil' can show unexpected behaviour when using float values due to inaccuracies in the float system and PHP tendency to freely limiting the number of digits it displays to the user.
It might like it is rounding up values that end in 0 (zero) but actually PHP is not showing that the value is not exactly the value it shows.

For example 13915 cannot be represented as exactly 13915 but becomes 
13916.0000000000018190 and 'ceil' will therefore round it up to 13916.

So if 'ceil' looks like it is not working correctly and rounding up figures that end in 0 (zero) then this might be the cause of this behaviour.
Adding a simple 'round' before the applying the 'ceil' solves this problem.
Here is a more accurate way to round on superior decimal.
function round_sup($nb, $precision) {
    $p = pow(10, $precision);
    return ceil(round($nb * $p, 1)) / $p;
}
$k = 10 * 4.98;          // k = 49.8
echo ceil($k * 10) / 10;  // 49.9  !!
echo round_sup($k, 1);  // 49.8
Remember that floating point precision means behavior can be "correct" - though not what you expect:
php > echo 100 * 1 * 0.07;
7
php > echo ceil(100 * 1 * 0.07);
8
Here is another way to use ceil for decimal numbers with precision: 
The code below rounds a value up to a nearest multiple, away from zero. The multiple does not have to be a integer. So you could round, say, to the nearest 25.4, allowing you to round measurements in mm to the nearest inch longer.

I originally developed this as an example of write-only code: to make the point that being cleverly terse might save clock ticks but wastes more in programmer time generating un-maintainable code.
The inline code above nests one conditional statement inside another. The value of y changes twice within the same line (three times, if you count the pre-increment). The value of each assignment is used to determine branching within the conditional statement.
How it works can more easily be seen from the expansion below:


Comparing the versions for speed: the in-line version is about three times faster than myCeilingLong() - but this is almost entirely down to function call overhead. 
Putting the in-line code inside the function: the difference in execution speed between myCeilingLong() and myCeilingShort() is around 1.5%.
ceil() is still around 25% faster than the in-line statement so if you are a speed hound your efforts might be better devoted to compiling your own library ...
IceKarma said: "If you want, say, 2.6 to round to 3, and -2.6 to round to -3, you want round(), which rounds away from zero."
That's not always true. round() doesn't work that way, like zomis2k said it just rounds up _or_ down to the nearest non-decimal number. However this should work. 
Just a quick note to be careful of, if you use the "round_up" code suggested by steve, i must warn you it isn't completely fool proof.
I have the following maths which is evaluated incorrectly
37.2000 - 6.2000 = 31.01
echo(round_up(37.2000 - 6.2000,2));
This returns the incorrect result, 31.01 which as any junior in mathematics knows...IS WRONG!
use with caution.
You can simply make a ceil with decimals function by using the round() function and add 0.05 to the value if the value must have 1 decimal accuracy, or 0.005 for 2 decimals accuracy, or 0.0005 for 3 decimals etc.
Example :
function ceilDec($input, $decimals)
{
  $subtract = 5 / pow(10, $decimals + 1);
  $output = round($input + $subtract, $decimals);
  return $output;
}

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)