(PHP 4, PHP 5, PHP 7, PHP 8) Show ceil — Round fractions up Descriptionceil(int|float $num): float Returns the next highest integer value by rounding up num if necessary. ParametersnumThe value to round Return Valuesnum rounded up to the next highest integer. The return value of ceil() is still of type float as the value range of float is usually bigger than that of int. Changelog
ExamplesExample #1 ceil() example
<?php Scott Weaver / scottmweaver * gmail ¶ 14 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 echo ceiling(0, 1000); // 0echo ceiling(1, 1); // 1000 echo ceiling(1001, 1000); // 2000 echo ceiling(1.27, 0.05); // 1.30?> eep2004 at ukr dot net ¶ 4 years ago
Caution! steve_phpnet // nanovox \\ com ¶ 17 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 echo round_up (56.77001, 2); // displays 56.78echo 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 ?> oktam ¶ 11 years ago
Actual behaviour: Workaround: eep2004 at ukr dot net ¶ 4 years ago
Caution! aaron at mind-design dot co dot uk ¶ 18 years ago Or for the terniary fans: <?phpfunction roundaway($num) { Gemini_13 at torba dot su ¶ 7 years ago $test = (1 - 0.7) * 10; $test_1 = ceil($test); var_dump($test_1); // float 4 themanwe at yahoo dot com ¶ 15 years ago float ceil function fCeil($val,$pressision=2){ php is the best ¶ 10 years ago Ceil for decimal numbers with precision: function ceil_dec($number,$precision,$separator) $ceil_number= array($numberpart[0],$numberpart[1]); echo ceil_dec(1.125,2,"."); //1.13 eg at pensio dot com ¶ 9 years ago Remember that floating point precision means behavior can be "correct" - though not what you expect: php > echo 100 * 1 * 0.07; roger_dupere at hotmail dot com ¶ 18 years ago Here is a navbar using the ceil function.
<?php
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>" ); } ?> Lexand ¶ 10 years ago
$k = 0.14 * 100; solution is in converting float number to string Example 1. Example 2. Example 3. useful for 'ceil' with precision capability rainfalling at yahoo dot com ¶ 18 years ago 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. <?phpfunction roundaway($num) { Bas Vijfwinkel ¶ 7 years ago
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. <?PHP <?PHP cedricaubert at gmail dot com ¶ 7 years ago Here is a more accurate way to round on superior decimal. function round_sup($nb, $precision) { $k = 10 * 4.98; // k = 49.8 alesegdia at gmail dot com ¶ 9 years ago Some people asking on rounding -1.5 to -2 and 1.5 to 2, the way is this: <?=round($var, 0, PHP_ROUND_HALF_UP)?> AndrewS ¶ 11 years ago 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 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 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 ... sebastien dot thevenaz at gmail dot com ¶ 10 years ago Here is another way to use ceil for decimal numbers with precision: <?php Anonymous ¶ 6 years ago 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. josoort at home dot nl ¶ 8 years ago 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) What does Ceil () function do?The ceil() function computes the smallest integer that is greater than or equal to x.
How do I find the ceil of a number?ceil() method. The Math. ceil() method returns the smallest (closest to negative infinity) double value which is greater than or equal to the parameter and has a value which is equal to a mathematical integer on the number line.
How do you represent a ceil function?The ceiling function (also known as the least integer function) of a real number x, denoted ⌈ x ⌉ , \lceil x\rceil, ⌈x⌉, is defined as the smallest integer that is not smaller than. x. x.
Which library is used for ceil () floor () functions?The Python ceil() function rounds a number up to the nearest integer, or whole number. Python floor() rounds decimals down to the nearest whole number. Both of these functions are part of the math Python library.
|