C语言floor():向下取整
floor() 是 C语言的一个标准库函数,定义在
floor() 函数的功能是向下取整,简单来讲就是求不大于 x 的最大整数。floor() 函数的原型如下:
【实例】以下的 C 语言程序演示了 floor() 函数的功能和用法。
输出结果为:
和其他数学库函数一样,在某些编译环境中,可能需要使用特定的链接选项来链接数学库,例如在 GCC 上使用
<math.h>
头文件中。floor() 函数的功能是向下取整,简单来讲就是求不大于 x 的最大整数。floor() 函数的原型如下:
double floor(double x);
参数
x:一个浮点数。返回值
返回不大于 x 的最大整数值:- 如果 x 的值是正整数,则 floor() 返回不大于该值的最大整数。
- 如果 x 的值是负整数,则 floor() 返回不大于该值的下一个更小的整数。
【实例】以下的 C 语言程序演示了 floor() 函数的功能和用法。
- #include <stdio.h>
- #include <math.h>
- int main() {
- double value1 = 12.34;
- double value2 = -12.34;
- printf("The floor of %.2f is %.2f\n", value1, floor(value1));
- printf("The floor of %.2f is %.2f\n", value2, floor(value2));
- return 0;
- }
The floor of 12.34 is 12.00
The floor of -12.34 is -13.00
和其他数学库函数一样,在某些编译环境中,可能需要使用特定的链接选项来链接数学库,例如在 GCC 上使用
-lm
。
gcc your_program.c -lm