How to use the floor function in C++?

In C++, the floor() function can be used to round down. The prototype of this function is defined in the header file and needs to be included when using it.

Here is an example code demonstrating how to use the floor() function for rounding down:

#include <iostream>
#include <cmath>

int main() {
    double num = 3.14;
    double result = floor(num);

    std::cout << "向下取整后的结果为: " << result << std::endl;

    return 0;
}

In the example above, we declare a variable “num” of type double and assign it the value 3.14. Next, we use the floor() function to round down the value and store it in the “result” variable, then output the result.

Executing the above code will yield the following output:

向下取整后的结果为: 3
Leave a Reply 0

Your email address will not be published. Required fields are marked *