PHP 8.4.2 Released!

ceil

(PHP 4, PHP 5, PHP 7, PHP 8)

ceil进一法取整

说明

ceil(int|float $num): float

如果有必要,通过对 num 向上取整返回下一个最高的整数。

参数

num

要进一法取整的值

返回值

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

更新日志

版本 说明
8.0.0 num 不再接受支持数字转换的内部对象。

示例

示例 #1 ceil() 示例

<?php
echo ceil(4.3); // 5
echo ceil(9.999); // 10
echo ceil(-3.14); // -3
?>

参见

添加备注

用户贡献的备注 5 notes

up
87
Scott Weaver / scottmweaver * gmail
16 years ago
I needed this and couldn't find it so I thought someone else wouldn't have to look through a bunch of Google results-

<?php

// duplicates m$ excel's ceiling function
if( !function_exists('ceiling') )
{
function
ceiling($number, $significance = 1)
{
return (
is_numeric($number) && is_numeric($significance) ) ? (ceil($number/$significance)*$significance) : false;
}
}

echo
ceiling(0, 1000); // 0
echo ceiling(1, 1); // 1000
echo ceiling(1001, 1000); // 2000
echo ceiling(1.27, 0.05); // 1.30

?>
up
49
eep2004 at ukr dot net
6 years ago
Caution!
<?php
$value
= 77.4;
echo
ceil($value * 100) / 100; // 77.41 - WRONG!
echo ceil(round($value * 100)) / 100; // 77.4 - OK!
up
24
steve_phpnet // nanovox \\ com
19 years ago
I couldn't find any functions to do what ceiling does while still leaving I specified number of decimal places, so I wrote a couple functions myself. round_up is like ceil but allows you to specify a number of decimal places. round_out does the same, but rounds away from zero.

<?php
// round_up:
// rounds up a float to a specified number of decimal places
// (basically acts like ceil() but allows for decimal places)
function round_up ($value, $places=0) {
if (
$places < 0) { $places = 0; }
$mult = pow(10, $places);
return
ceil($value * $mult) / $mult;
}

// round_out:
// rounds a float away from zero to a specified number of decimal places
function round_out ($value, $places=0) {
if (
$places < 0) { $places = 0; }
$mult = pow(10, $places);
return (
$value >= 0 ? ceil($value * $mult):floor($value * $mult)) / $mult;
}

echo
round_up (56.77001, 2); // displays 56.78
echo round_up (-0.453001, 4); // displays -0.453
echo round_out (56.77001, 2); // displays 56.78
echo round_out (-0.453001, 4); // displays -0.4531
?>
up
14
oktam
13 years ago
Actual behaviour:
echo ceil(-0.1); //result "-0" but i expect "0"

Workaround:
echo ceil(-0.1)+0; //result "0"
up
8
frozenfire at php dot net
12 years ago
Please see http://www.php.net/manual/en/language.types.float.php for information regarding floating point precision issues.
To Top