前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >C语言(C++)中:详解floor函数、ceil函数和round函数

C语言(C++)中:详解floor函数、ceil函数和round函数

作者头像
用户7886150
修改2021-02-11 18:16:43
修改2021-02-11 18:16:43
4.7K0
举报
文章被收录于专栏:bit哲学院bit哲学院

参考链接: C++ ceil()

C语言中 

1.floor函数 

功能:把一个小数向下取整

      即就是如果数是2.2 ,那向下取整的结果就为2.000000

原型:double floor(doube x);

    参数解释:

        x:是需要计算的数

返回值:

    成功:返回一个double类型的数,此数默认有6位小数

    无失败的返回值

头文件:#include<math.h>

示例  floor函数计算后的结果为double类型的: 

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

int main()

{

    double i = floor(2.2);

    double j = floor(-2.2);

    printf("The floor of 2.2 is %f\n", i);

    printf("The floor of 2.2 is %f\n", j);

    system("pause");

    return 0;

运行结果:    floor函数把转换后的结果强转为int类型的: 

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

int main()

{

    int i = floor(2.2);

    int j = floor(2.7);

    printf("i=%d,j=%d\n", i, j);

    system("pause");

    return 0;

运行结果:    ps:把计算结果强转为int后,会丢失精度 

2.ceil函数 

功能:把一个小数向上取整

      即就是如果数是2.2 ,那向上取整的结果就为3.000000

原型:double ceil(doube x);

    参数解释:

        x:是需要计算的数

返回值:

    成功:返回一个double类型的数,此数默认有6位小数

    无失败的返回值

头文件:#include<math.h>

示例  ceil函数计算的结果为double类型的: 

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

int main()

{

    double i = ceil(2.2);

    double j = ceil(-2.2);

    printf("The ceil of 2.2 is %f\n", i);

    printf("The ceil of 2.2 is %f\n", j);

    system("pause");

    return 0;

运行结果:   

ceil函数把计算后的结果强转为int类型的: 

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

int main()

{

    int i = ceil(2.2);

    int j = ceil(2.7);

    printf("i=%d,j=%d\n", i, j);

    system("pause");

    return 0;

运行结果:   

3.round函数 

功能:把一个小数四舍五入

      即就是如果数是2.2 ,那四舍五入的结果就为2

           如果数是2.5,那结果就是3

原型:double round(doube x);

    参数解释:

        x:是需要计算的数

头文件:#include<math.h>

示例 

round函数的计算结果为double类型的: 

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

int main()

{

    double i = round(2.2);

    double x = round(2.7);

    double j = round (-2.2);

    double y = round(-2.7);

    printf("The round of 2.2 is %f\n", i);

    printf("The round of 2.7 is %f\n", x);

    printf("The round of -2.2 is %f\n", j);

    printf("The round of -2.7 is %f\n", y);

    system("pause");

    return 0;

}

运行结果:   

C++中 

1.floor函数 

#include<iostream>

using namespace std;

int main()

{

    double i = floor(2.2);

    double j = floor(-2.2);

    cout << "The floor of 2.2 is " << i << endl;

    cout << "The floor of -2.2 is " << j << endl;

    system("pause");

    return 0;

运行结果:   

2.ceil函数 

#include<iostream>

using namespace std;

int main()

{

    double i = ceil(2.2);

    double j = ceil(-2.2);

    cout << "The ceil of 2.2 is " << i << endl;

    cout << "The ceil of -2.2 is " << j << endl;

    system("pause");

    return 0;

}

运行结果:   

3.round函数 

#include<iostream>

using namespace std;

int main()

{

    double i = round(2.2);

    double x = round(2.7);

    double j = round(-2.2);

    double y = round(-2.7);

    cout << "The round of 2.2 is " << i << endl;

    cout << "The round of 2.7 is " << x << endl;

    cout << "The round of -2.2 is " << j << endl;

    cout << "The round of -2.7 is " << y << endl;

    system("pause");

    return 0;

运行结果:

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档