在大多数编程语言中,默认取整方式为截断取整,而在数学运算中,我们经常需要使用其它取整方式。在C++ cmath中包含了这三类取整函数:向上取整ceil(),向下取整floor()和四舍五入round()

向上取整 ceil()

函数原型:double ceil(doube x)
输入一个浮点值,返回其向上取整结果,默认为一个有六位小数的double值

例:

    int i = ceil(2.2);
    int j = ceil(-2.2);

    printf("The ceil of 2.2 is %d\n", i);
    printf("The ceil of -2.2 is %d\n", j);
    return 0;

输出结果为

The ceil of 2.2 is 3
The ceil of 2.2 is -2

向下取整 floor()

函数原型:double floor(doube x)
输入一个浮点值,返回其向下取整结果,默认为一个有六位小数的double值

例:

    int i = floor(2.2);
    int j = floor(-2.2);

    printf("The floor of 2.2 is %d\n", i);
    printf("The floor of -2.2 is %d\n", j);
    return 0;

输出结果为

The floor of 2.2 is 2
The floor of -2.2 is -3

四舍五入 round()

函数原型:double floor(doube x)
输入一个浮点值,对其进行四舍五入,默认返回值为double类型

    int i = round(2.2);
    int j = round(2.7);

    printf("The round of 2.2 is %d\n", i);
    printf("The round of 2.7 is %d\n", j);
    return 0;

输出结果为

The round of 2.2 is 2
The round of 2.7 is 3

参考阅读:https://blog.csdn.net/dangzhangjing97/article/details/81279862