ceil
ceil(float $num): float
Rounds fractions up (ceiling).
C C23
C23
#include <math.h>
#include <stdio.h>
int main(void) {
printf("%g\n", ceil(4.2)); // smallest integer >= the argument
printf("%g\n", ceil(-4.2));
return 0;
}
Output
5 -4
C++ C++26
C++26
#include <cmath>
#include <cstdio>
int main() {
std::printf("%g\n", std::ceil(4.2));
std::printf("%g\n", std::ceil(-4.2)); // ceiling heads to +infinity, not away from zero
return 0;
}
Output
5 -4
C# v11.0.100
v11.0.100
Console.WriteLine(Math.Ceiling(4.2)); // returns double, prints as "5"
Console.WriteLine(Math.Ceiling(-4.2));
Output
5 -4
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Ceil(4.2))
fmt.Println(math.Ceil(-4.2))
}
Output
5 -4
Java v27.0.1
v27.0.1
public class Main {
public static void main(String[] args) {
System.out.println((long) Math.ceil(4.2));
System.out.println((long) Math.ceil(-4.2));
}
}
Output
5 -4
JavaScript v26.13.1
v26.13.1
console.log(Math.ceil(4.2));
console.log(Math.ceil(-4.2));
Output
5 -4
PHP v8.6.0
v8.6.0
echo ceil(4.2), "\n";
echo ceil(-4.2);
Output
5 -4
Python v3.15.2
v3.15.2
import math
print(math.ceil(4.2))
print(math.ceil(-4.2))
Output
5 -4
Rust v1.99.1
v1.99.1
fn main() {
println!("{}", 4.2f64.ceil()); // ceil keeps f64; Display prints "5"
println!("{}", (-4.2f64).ceil());
}
Output
5 -4
TypeScript v26.13.1
v26.13.1
console.log(Math.ceil(4.2));
console.log(Math.ceil(-4.2));
Output
5 -4
Language differences
C: ceil rounds toward +infinity (ceil(4.2) == 5, ceil(-4.2) == -4 — for negatives that is not "away from zero"); it pairs with floor (toward -infinity): page counts use ceil, bucketing/floor alignment uses floor, and round has its own entry; the semantics match everywhere.
C++: std::ceil.
C#: Math.Ceiling.
Go: math.Ceil.
Java: Math.ceil; printing a double leaves a trailing .0, so the example casts to long to keep the output uniform.
JavaScript: Math.ceil.
PHP: ceil, returning a float type (with an integral value).
Python: math.ceil returns an int directly (the others return float types).
Rust: f64::ceil.
TypeScript: same as JavaScript — Math.ceil.