Math 函数
在线手册:中文  英文

ceil

(PHP 4, PHP 5)

ceil进一法取整

说明

float ceil ( float $value )

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

参数

value

要进一法取整的值

返回值

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

范例

Example #1 ceil() 例子

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

参见


Math 函数
在线手册:中文  英文

用户评论:

alesegdia at gmail dot com (2013-06-16 19:00:37)

Some people asking on rounding -1.5 to -2 and 1.5 to 2, the way is this:

<?=round($var0PHP_ROUND_HALF_UP)?>

See round() doc for more information on this.

morris dot mwanga at gmail dot com (2013-02-12 20:18:07)

Using the ceil does not work on negative numbers;
print_r(ceil(-2.9)) // -2
If you want to get the absolute positve, use the abs function
print_r(ceil(abs(-2.9))) // 3

sebastien dot thevenaz at gmail dot com (2012-07-30 18:20:51)

Here is another way to use ceil for decimal numbers with precision:

<?php
    
function ceil_dec($number$precision)
    {
        
$coefficient pow(10,$precision);
        return 
ceil($number*$coefficient)/$coefficient;
    }
?>

php is the best (2012-04-20 06:29:54)

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

Lexand (2012-03-30 12:15:12)

$k = 0.14 * 100;
echo ceil($k); // results 15
solution is in converting float number to string
Example 1.
echo ceil ("{$k}"); // results 14
Example 2.
$totalSum1 = 102.1568;
$k = $totalSum1 / 100;
echo ceil ("{$k}"); // results 102.16
Example 3.
$totalSum2 = 102.15;
$k = $totalSum1 / 100;
echo ceil ("{$k}"); // results 102.15
useful for 'ceil' with precision capability

frozenfire at php dot net (2012-02-06 02:38:10)

Please see http://www.php.net/manual/en/language.types.float.php for information regarding floating point precision issues.

oktam (2011-05-10 03:12:44)

Actual behaviour:
echo ceil(-0.1); //result "-0" but i expect "0"
Workaround:
echo ceil(-0.1)+0; //result "0"

AndrewS (2011-03-07 04:55:11)

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.

<?php
// $x is the variable
// $c is the base multiple to round to, away from zero
$result =  ( ($y $x/$c) == ($y = (int)$y) ) ? $x : ( $x>=?++$y:--$y)*$c ;
?>

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:

<?php
function myCeilingLong($x,$c)
{
    
// $x is variable
    // $c is ceiling multiple
    
$a $x/$c ;
    
$b = (int)$a ;
    if (
$a == $b)
        return 
$x ;  // x is already a multiple of c;
    
else
    {
        if (
$x>=0)
            return (
$b+1)*$c ;  // return ((int)(x/c)+1 ) * c
        
else
            return (
$b-1)*$c ;  // return ((int)(x/c)-1 ) * c
    
}
}
?>

<?php
function myCeilingShort($x,$c)
{
    return ( (
$y $x/$c) == ($y = (int)$y) ) ? $x : ( $x>=?++$y:--$y)*$c ;
}
?>

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 ...

that_cow at gmail dot com (2009-01-13 16:05:17)

Scott Weaver / scottmweaver * gmail I am not sure if this was a typo or what but in your example
ceiling(1,1) is not 1000, it is 1

Chevy (2008-12-30 00:54:47)

Quick and dirty `ceil` type function with precision capability.

<?php
function ceiling($value$precision 0) {
    return 
ceil($value pow(10$precision)) / pow(10$precision);
}
?>

agadret at terra dot com dot br (2008-12-14 10:52:18)

Be aware that
echo 5*0.2*7; // results 7
echo ceil (5*0.2*7); // results 7
echo ceil (5*(0.2*7)); // results 8

Scott Weaver / scottmweaver * gmail (2008-08-29 10:46:51)

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(01000);     // 0
echo ceiling(11);        // 1000
echo ceiling(10011000);  // 2000
echo ceiling(1.270.05);  // 1.30

?>

benjamwelker * gmail (2008-05-29 09:57:11)

@ zariok

that function is nice, but it only works for positive numbers, causing negative numbers to be grossly incorrect.

e.g.- 

round_up(4.765, 2) => 4.77 as expected
round_up(-4.765, 2) => -3.23

a couple modified versions of your function (depending on which one you really want):

<?php

// rounds towards positive infinity
function round_up($value$precision 0) {
    
$sign = (<= $value) ? +: -1;
    
$amt explode('.'$value);
    
$precision = (int) $precision;
    
    if (
strlen($amt[1]) > $precision) {
        
$next = (int) substr($amt[1], $precision);
        
$amt[1] = (float) (('.'.substr($amt[1], 0$precision)) * $sign);
        
        if (
!= $next) {
            if (+
== $sign) {
                
$amt[1] = $amt[1] + (float) (('.'.str_repeat('0'$precision 1).'1') * $sign);
            }
        }
    }
    else {
        
$amt[1] = (float) (('.'.$amt[1]) * $sign);
    }
    
    return 
$amt[0] + $amt[1];
}

// rounds away from zero
function round_out($value$precision 0) {
    
$sign = (<= $value) ? +: -1;
    
$amt explode('.'$value);
    
$precision = (int) $precision;
    
    if (
strlen($amt[1]) > $precision) {
        
$next = (int) substr($amt[1], $precision);
        
$amt[1] = (float) (('.'.substr($amt[1], 0$precision)) * $sign);
        
        if (
!= $next) {
            
$amt[1] = $amt[1] + (float) (('.'.str_repeat('0'$precision 1).'1') * $sign);
        }
    }
    else {
        
$amt[1] = (float) (('.'.$amt[1]) * $sign);
    }
    
    return 
$amt[0] + $amt[1];
}

?>

InsideR(); (2007-08-09 18:01:56)

Just to comment on zariok's comment (which is right below mine), his problem is likely due to the fact that decimal numbers (such as 0.5500) cannot be exactly represented in binary (and hence computers can't precisely determine that 0.5500 * 100 = 55).
This feature is great when you know that your result is going to be nowhere near an integer (for example, finding ceil(1/3) will confidently give a 1). However in situations like his, this is probably not the better function to use.

zariok (2007-08-09 07:28:28)

the fCeil and round_up listed below are not reliable. This could be due to a broken ceil function:
CODE:
function fCeil($val,$pressision=2){
$p = pow(10,$pressision);
$val = $val*$p;
$val = ceil($val);
return $val /$p;
}
print "fCeil: ".fCeil("0.5500",2)."\n";
print "ceil: ".ceil("55.00")."\n";
print "ceil: ".ceil(0.5500 * 100)."\n"; // should be interpreted as ceil(55);
OUTPUT:
fCeil: 0.56
ceil: 55
ceil: 56
Tested: PHP v5.2.2, v5.1.6, v5.0.4 CLI
Quick function I used as replacement:
CODE:
function round_up ($value, $precision=2) {
$amt = explode(".", $value);
if(strlen($amt[1]) > $precision) {
$next = (int)substr($amt[1],$precision);
$amt[1] = (float)(".".substr($amt[1],0,$precision));
if($next != 0) {
$rUp = "";
for($x=1;$x<$precision;$x++) $rUp .= "0";
$amt[1] = $amt[1] + (float)(".".$rUp."1");
}
}
else {
$amt[1] = (float)(".".$amt[1]);
}
return $amt[0]+$amt[1];
}
print round_up("0.5500",2)."\n";
print round_up("2.4320",2)."\n";
print "\nprecision: 2\n";
print round_up("0.5",2)."\n";
print round_up("0.05",2)."\n";
print round_up("0.050",2)."\n";
print round_up("0.0501", 2)."\n";
print round_up("0.0500000000001", 2)."\n";
print "\nprecision: 3\n";
print round_up("0.5",3)."\n";
print round_up("0.05",3)."\n";
print round_up("0.050",3)."\n";
print round_up("0.0501",3)."\n";
print round_up("0.0500000000001",3)."\n";
OUTPUT:
0.55
2.44
precision: 2
0.5
0.05
0.05
0.06
0.06
precision: 3
0.5
0.05
0.05
0.051
0.051

themanwe at yahoo dot com (2007-03-20 11:35:13)

float ceil
function fCeil($val,$pressision=2){
$p = pow(10,$pressision);
$val = $val*$p;
$val = ceil($val);
return $val /$p;
}

ermolaeva_elena at mail dot ru (2005-12-20 07:27:28)

To round a number up to the nearest power of 10,
I've used
= ceil(intval($val)/10)*10;

nobody (2005-11-22 21:00:16)

Here's a more simple one to do ceil to nearest 10:
function ceilpow10(val) {
if (val % 10 == 0) return val;
return val + (10 - (val % 10));
}

schmad at miller dash group dot net (2005-04-18 21:38:17)

To round a number up to the nearest power of 10 use this simple procedure:
$multiplier = .1;
while($number>1)
{
$number /= 10;
$multiplier *= 10;
}
$number = ceil($number) * $multiplier;

coxswain at navaldomination dot com (2005-03-16 14:06:10)

steve_phpnet // nanovox \\ com wouldn't:

<?php
$ceil  
ceil(4.67 10) / 10;
?>

work just as well?

steve_phpnet // nanovox \\ com (2005-02-28 12:40:48)

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 >= ceil($value $mult):floor($value $mult)) / $mult;
 }

 echo 
round_up (56.770012); // displays 56.78
 
echo round_up (-0.4530014); // displays -0.453
 
echo round_out (56.770012); // displays 56.78
 
echo round_out (-0.4530014); // displays -0.4531
?>

aaron at mind-design dot co dot uk (2004-07-21 13:10:15)

Or for the terniary fans:

<?php

function roundaway($num) {
   return((
$num 0) ? ceil($num) : floor($num));
}

?>

Slightly pointless, but there you have it, in one line only..

rainfalling at yahoo dot com (2004-04-22 05:51:20)

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.

<?php

function roundaway($num) {
    if (
$num 0)
      return 
ceil($num);
    elseif (
$num 0)
      return 
floor($num);
    elseif (
$num == 0)
      return 
0;
}

?>

roger_dupere at hotmail dot com (2003-11-10 02:02:55)

Here is a navbar using the ceil function.

<?php
 
function navbar($num_rows,$page,$link) {
   
$nbrlink 10/* Number of link to display per page */
   
$page = (int) $page/* Page now displayed */
   
$num_rows = (int) $num_rows;

   if( 
$num_rows ) {
     
$total_page ceil$num_rows $nbrlink );

     for( 
$i=1;$i<$total_page+1;$i++ ) {
       if( 
$i == $page ) {
         
$ret .= " <b>$i</b> ";
       } else {
         if( 
strstr$link,"?" ) ) {
           
$ret .= " <a href=\"$link&page=$i\">$i</a> ";
         } else {
           
$ret .= " <a href=\"$link?page=$i\">$i</a> ";
         }
       }
     }

     return 
$ret;
   }
 }
  
/* Let say that $num_rows content the numbre of rows of your sql query */
  
$navbar navbar$num_rows$page"listmovie.php?id=$id);

  if( 
$navbar != null || $navbar != "" ) {
    print( 
"<p><div align=\"center\">$navbar</div></p>" );
  }
?>

易百教程