C语言(C++)中:详解floor函数、ceil函数和round函数 |
您所在的位置:网站首页 › 数据库cell函数 › C语言(C++)中:详解floor函数、ceil函数和round函数 |
C语言中
1.floor函数
功能:把一个小数向下取整
即就是如果数是2.2 ,那向下取整的结果就为2.000000
原型:double floor(doube x);
参数解释:
x:是需要计算的数
返回值:
成功:返回一个double类型的数,此数默认有6位小数
无失败的返回值
头文件:#include
示例 floor函数计算后的结果为double类型的: #include #include #include 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; }运行结果: 运行结果: 示例 ceil函数计算的结果为double类型的: #include #include #include 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 #include #include int main() { int i = ceil(2.2); int j = ceil(2.7); printf("i=%d,j=%d\n", i, j); system("pause"); return 0; }运行结果: 示例 round函数的计算结果为double类型的: #include #include #include 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; }运行结果: |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |