[C/C++语法]—取整函数

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
发布了20 篇原创文章 · 获赞 2 · 访问量 939

猜你喜欢

转载自blog.csdn.net/zhbbbbbb/article/details/103560842