floor()函数
floor函数为向下取整函数,返回的是小于或小于等于x的最大整数,也就是向负无穷取整
函数原型:double floor(double)
floor函数的头文件为<math.h>
例如:
#include <stdio.h>
#include <math.h>
int main(void)
{
double a;
while(~scanf("%lf", &a))
{
double ans1;
ans1 = floor(a);
printf("floor = %lf\n", ans1);
}
return 0;
}
ceil()函数
ceil函数为向上取整函数,返回的是大于或大于等于x的最大整数,也就是向正无穷取整
函数原型: double ceil(double x)
ceil函数的头文件为<math.h>
例如:
#include <stdio.h>
#include <math.h>
int main(void)
{
double a;
while(~scanf("%lf", &a))
{
double ans1;
ans1 = ceil(a);
printf("ceil= %lf\n", ans1);
}
return 0;
}
round()函数
round()函数为四舍五入函数
函数原型:double round(double x)
round()函数的头文件为<math.h>
例如:
#include <stdio.h>
#include <math.h>
int main(void)
{
double a;
while(~scanf("%lf", &a))
{
double ans1;
ans1 = round(a);
printf("round = %lf\n", ans1);
}
return 0;
}
总结
函数名称 | 函数说明 | 2.9 | 2.1 | -2.9 | -2.1 |
---|---|---|---|---|---|
floor() | 向下(负无穷)取整函数 | 2.0 | 2.0 | -3.0 | -3.0 |
ceil() | 向上(正无穷)取整函数 | 3.0 | 3.0 | -2 | -2 |
round() | 四舍五入 | 3.0 | 2.0 | -3.0 | -2.0 |