C++ std::ceil() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

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

ParameterDescription
xRequired. The number to round up. It can be float, double, long double, or an integer type (converted to double).

Return Value

Return ValueDescription
Rounded-up valueThe smallest whole number that is not less than x, as a floating-point type.
-0.0For a small negative number such as -0.5, the result is -0.0.
xIf 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;
}

How It Works

  1. The loop gives each number in the array to std::ceil().
  2. 3.2 goes up to 4. 3.0 is already whole, so it stays 3.
  3. -3.2 goes up toward zero, to -3. -0.5 goes up to -0, which is negative zero, and cout prints it with the minus sign.

Example 2 — How Many Pages Do You Need

With 10 items per page, 23 items need 3 pages. Divide, then round up with ceil().

C++
#include <cmath>
#include <iostream>

int main() {
    int perPage = 10;

    for (int items : {23, 30, 31}) {
        int pages = static_cast<int>(std::ceil(static_cast<double>(items) / perPage));
        std::cout << items << " items -> " << pages << " page(s)\n";
    }
    return 0;
}

How It Works

  1. The cast to double is important. Dividing two int values would drop the decimal part before ceil() could see it.
  2. 23 / 10 is 2.3, and ceil() turns it into 3. A page is needed for the last 3 items.
  3. 30 / 10 is exactly 3, so no extra page is added. 31 items give 3.1 and need 4 pages.

Example 3 — Round a Price Up to the Next 5 Cents

Multiply by 20, round up, and divide by 20. This rounds up to the next 0.05.

C++
#include <cmath>
#include <iomanip>
#include <iostream>

int main() {
    double prices[] = {2.31, 4.00, 7.01};

    std::cout << std::fixed << std::setprecision(2);
    for (double p : prices) {
        double rounded = std::ceil(p * 20.0) / 20.0;
        std::cout << p << " -> " << rounded << '\n';
    }
    return 0;
}

How It Works

  1. There are 20 steps of 0.05 in one unit, so multiplying by 20 turns the price into a count of steps.
  2. 2.31 * 20 is 46.2, and ceil() makes it 47. Dividing by 20 gives 2.35.
  3. 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.

Next: copysign()

Keep going through the <cmath> functions. Take the size of one number and the sign of another.

copysign() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful