这篇文章主要介绍了C++使用floorround实现保留小数点后两位的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
C++四舍五入保留小数点后两位
示例
#include <iostream>
using namespace std;
int main()
{
double i = 2.235687;
double j = round(i * 100) / 100;
cout << "The original number is " << i << endl;
cout << "The keep two decimal of 2.235687 is " << j << endl;
system("pause");
return 0;
}运行结果
函数解析见下面
1、floor函数
功能:把一个小数向下取整 即就是如果数是2.2,那向下取整的结果就为2.000000
原型:double floor(doube x);
参数解释:
x:是需要计算的数
示例
#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函数
功能:把一个小数向上取整
即就是如果数是2.2,那向下取整的结果就为3.000000
原型:double ceil(doube x);
参数解释:
x:是需要计算的数
示例
#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函数
功能:把一个小数四舍五入 即就是如果数是2.2,那向下取整的结果就为2 如果数是2.5,那向上取整的结果就为3
原型:double round(doube x);
参数解释:
x:是需要计算的数
示例
#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;
}运行结果
到此这篇关于C++详解使用floor&ceil&round实现保留小数点后两位的文章就介绍到这了,更多相关C++ floor ceil round内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
本文标题为:C++详解使用floor&ceil&round实现保留小数点后两位
- c++ const 成员函数,返回一个 const 指针.但是返回的指针是什么类型的 const? 2022-10-11
- Qt计时器使用方法详解 2023-05-30
- C语言qsort()函数的使用方法详解 2023-04-26
- 详解C语言中sizeof如何在自定义函数中正常工作 2023-04-09
- C++ 数据结构超详细讲解顺序表 2023-03-25
- 我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上? 2022-10-30
- Easyx实现扫雷游戏 2023-02-06
- C语言详解float类型在内存中的存储方式 2023-03-27
- C语言手把手带你掌握带头双向循环链表 2023-04-03
- ubuntu下C/C++获取剩余内存 2023-09-18
