In C++, std::ceil(x) rounds a number up. The name is short for "ceiling". It returns the smallest whole number that is not less than x. ceil(3.2) is 4, and ceil(3.0) stays 3.
"Up" always means toward bigger numbers, so it goes toward zero for negatives: ceil(-3.2) is -3, not -4. The result is a double with no fractional part, so cast it to int when you need an integer.
std::ceil() is declared in <cmath> and is part of the group Rounding. Rounding functions turn a fractional number into a whole number. They differ in which way they go for negative numbers and for ties such as 2.5.
Syntax
Include <cmath> and call std::ceil. Start with these lines.
C++
double a = std::ceil(3.2); // double version
float b = std::ceil(3.2f); // float version
Call
What it does
std::ceil(3.2)
Passes a double and returns a double. Here the result is 4.
std::ceil(3.2f)
Passes a float, so the float overload runs and returns a float.
std::ceil(3)
An integer is converted to double. Returns 3 as a double.
Write std::ceil so the C++ overloads for float, double, and long double are used.
Library declarations
C++
float ceil(float x);
double ceil(double x);
long double ceil(long double x);
double ceil(Integer x); // for integer types
Parameters
Parameter
Description
x
Required. The number to round up. It can be float, double, long double, or an integer type (converted to double).
Return Value
Return Value
Description
Rounded-up value
The smallest whole number that is not less than x, as a floating-point type.
-0.0
For a small negative number such as -0.5, the result is -0.0.
x
If x is already a whole number, inf, or NaN, it is returned as it is.
C++ Examples
Example 1 — Round Several Numbers Up
Round a few numbers up. A whole number stays the same, and a small negative number becomes negative zero.
C++
#include <cmath>
#include <iostream>
int main() {
double values[] = {3.2, 3.0, -3.2, -0.5};
for (double v : values) {
std::cout << "ceil(" << v << ") = " << std::ceil(v) << '\n';
}
return 0;
}
There are 20 steps of 0.05 in one unit, so multiplying by 20 turns the price into a count of steps.
2.31 * 20 is 46.2, and ceil() makes it 47. Dividing by 20 gives 2.35.
4.00 * 20 is exactly 80, so it stays at 4.00. 7.01 gives 140.2, then 141, then 7.05.
Frequently Asked Questions
std::ceil(x) rounds x up to the smallest whole number that is greater than or equal to x. So ceil(3.2) is 4 and ceil(-3.2) is -3.
The rounded-up value as a floating-point number (not an int). ceil(3.2) returns 4.0, and a number that is already whole is returned unchanged.
ceil() rounds up, toward bigger numbers, and floor() rounds down, toward smaller numbers. For 3.2, ceil gives 4 and floor gives 3. For -3.2, ceil gives -3 and floor gives -4.
No. It returns a floating-point number such as 4.0. Cast it with static_cast<int>(...) or use std::lround() if you need an integer type.
Include <cmath> and write std::ceil(). That always works and picks the C++ overloads. The global name ::ceil often works too, but the standard does not promise it, and it may only take a double.