C++ 中的 floor 函数向下取整一个浮点数,舍弃其小数部分,将其转换为最接近的整数,即 Math.floor()。

C++ 中 floor 的含义
floor 是 C++ 中的数学函数,用于向下取整。它将一个浮点数向下取整到最接近的整数,舍弃小数部分。
如何使用 floor
floor 函数使用以下语法:
立即学习“C++免费学习笔记(深入)”;
<code class="cpp">double floor(double x);</code>
其中:
-
x是要取整的浮点数。 - 函数返回向下取整后的整数。
示例
以下示例演示了如何使用 floor 函数:
<code class="cpp">#include <iostream>
#include <cmath>
int main() {
double x = 3.14;
int result = floor(x);
std::cout << "Downward integer of " << x << " is " << result << std::endl;
return 0;
}</code>输出:
<code>Downward integer of 3.14 is 3</code>
注意事项
- floor 函数只适用于浮点数,不适用于整数。
- floor 函数不会影响原始浮点数的值。
- floor 函数可以结合其他数学函数使用,例如 ceil 和 round。











