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.
ceil
(PHP 4, PHP 5)
ceil — 端数の切り上げ
説明
float ceil ( float $value )value の次に大きい整数値を返します。
パラメータ
- value
丸める値。
返り値
value の次に大きい整数値を返します。 ceil() の返り値は float 型と なります。これは、float 値の範囲は通常 int よりも広いためです。
例
参考
| floor() |
| round() |
ceil
InsideR();
10-Aug-2007 10:01
10-Aug-2007 10:01
zariok
09-Aug-2007 11:28
09-Aug-2007 11: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
21-Mar-2007 04:35
21-Mar-2007 04:35
float ceil
function fCeil($val,$pressision=2){
$p = pow(10,$pressision);
$val = $val*$p;
$val = ceil($val);
return $val /$p;
}
admin("AT")dbss('DOT')dk
21-Jan-2007 07:31
21-Jan-2007 07:31
This note can be uset to eg. page shift.
$GbogRes = mysql_query("SELECT * FROM table WHERE felt = userid");
$CountRow = mysql_num_rows($GbogRes);
echo ceil($CountRow / 10);
so can you look X page you can shift.
rjones at i-ps dot net
05-Mar-2006 09:19
05-Mar-2006 09:19
To eliftymes below:
To check if a numer is divisible by another number just check if the modulus is zero:
if ($bigger_number % $lower_number == 0)
{
echo "Can be wholey divided<br />";
}
else
{
echo "Not wholey divisible";
}
tom pittlik
28-Feb-2006 09:02
28-Feb-2006 09:02
ceil() is a useful way to quickly format bogus shopping cart quantities:
<?
ceil("str"); // 0
ceil("001"); // 1
ceil("0.1"); // 1
ceil("a34"); // 0
ceil("2fg"); // 2
ceil("$%%"); // 0
?>
eliftymes at gmail dot com
24-Feb-2006 05:20
24-Feb-2006 05:20
Here's a little function I wrote that checks if a number is divisible by another number.
function check_divisible($number, $divider) {
if(ceil($number/$divider) == $number/$divider) {
return TRUE;
} else {
return FALSE;
}
}
Useage would be as so.
$bigger_number=10;
$smaller_number=2;
if(check_divisible($bigger_number, $smaller_number)) {
echo "$bigger_number is divisible by $smaller_number!";
} else {
echo "$bigger_number is NOT divisible by $smaller_number!";
}
It's possible uses would be for:
Every 3rd, fourth, fifth, or whatever row in a table could be treated uniquely
Simple games...
Teaching your little kid math.
ermolaeva_elena at mail dot ru
21-Dec-2005 12:27
21-Dec-2005 12:27
To round a number up to the nearest power of 10,
I've used
= ceil(intval($val)/10)*10;
nobody
23-Nov-2005 02:00
23-Nov-2005 02:00
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
19-Apr-2005 01:38
19-Apr-2005 01:38
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
17-Mar-2005 07:06
17-Mar-2005 07:06
steve_phpnet // nanovox \\ com wouldn't:
<?php
$ceil = ceil(4.67 * 10) / 10;
?>
work just as well?
steve_phpnet // nanovox \\ com
01-Mar-2005 05:40
01-Mar-2005 05:40
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
?>
sven at plus dot hr
10-Feb-2005 08:59
10-Feb-2005 08:59
function roundaway($num) {
switch($num) {
case ($num > 0):
$n = ceil($num);
break;
case ($num < 0):
$n = floor($num);
break;
case ($num == 0):
$n = 0;
break;
}
return $n;
}
aaron at mind-design dot co dot uk
22-Jul-2004 05:10
22-Jul-2004 05:10
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
22-Apr-2004 09:51
22-Apr-2004 09:51
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
10-Nov-2003 07:02
10-Nov-2003 07:02
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 > 0 ) {
$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>" );
}
?>
zomis2k at hotmail dot com
23-May-2003 03:24
23-May-2003 03:24
>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.
round() does not always round away from zero
round(2.4) = 2
round(2.6) = 3
round(-2.4) = -2
round(-2.6) = -3
round() rounds number to nearest non-decimal number.
IceKarma
22-Apr-2003 09:31
22-Apr-2003 09:31
ceil() rounds towards positive infinity ("up"), floor() rounds towards negative infinity ("down").
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.
clx at deim dot net
12-Jun-2002 05:55
12-Jun-2002 05:55
<?php
echo ceil(2.6); // will output "3"
echo ceil(-2.6); // will output "-2"
?>