C++ 保留几位小数
2016-04-19 17:19
447 查看
#include<iostream>
#include<iomanip> //头文件不可少
using namespace std;
int main()
{
double a = 3.14159;
cout << setiosflags(ios::fixed) << setprecision(0) << a << endl; //保留0位小数
cout << setiosflags(ios::fixed) << setprecision(1) << a << endl; //保留1位小数
cout << setiosflags(ios::fixed) << setprecision(2) << a << endl; //保留2位小数
cout << setiosflags(ios::fixed) << setprecision(3) << a << endl; //保留3位小数,不仅仅截断字符,还可以四舍五入
cout << setiosflags(ios::fixed) << setprecision(4) << a << endl; //保留4位小数
cout << setiosflags(ios::fixed) << setprecision(5) << a << endl; //保留5位小数
cout << setiosflags(ios::fixed) << setprecision(6) << a << endl; //保留6位小数
return 0;
}
输出:
#include<iomanip> //头文件不可少
using namespace std;
int main()
{
double a = 3.14159;
cout << setiosflags(ios::fixed) << setprecision(0) << a << endl; //保留0位小数
cout << setiosflags(ios::fixed) << setprecision(1) << a << endl; //保留1位小数
cout << setiosflags(ios::fixed) << setprecision(2) << a << endl; //保留2位小数
cout << setiosflags(ios::fixed) << setprecision(3) << a << endl; //保留3位小数,不仅仅截断字符,还可以四舍五入
cout << setiosflags(ios::fixed) << setprecision(4) << a << endl; //保留4位小数
cout << setiosflags(ios::fixed) << setprecision(5) << a << endl; //保留5位小数
cout << setiosflags(ios::fixed) << setprecision(6) << a << endl; //保留6位小数
return 0;
}
输出:
相关文章推荐
- 《C++primer》读书笔记三
- C语言易错和易忽视点
- STL—迭代器相关类型
- 9、ROS使用C++编写一个简单的Service和Client
- 8、ROS使用C++编写一个简单的Publisher和Subscriber
- C++中重载强制类型转换
- C++新特性学习笔记
- VC++中的DDX和DDV
- 解决VC++6.0 error C2065: 'ODS_HOTLIGHT' : undeclared identifier问题
- Leetcode 4. Median of Two Sorted Arrays
- C++连接SQL数据库第一步 系统配置
- C++ int、string等常见类型转换
- 【转】如何用 C/C++ 求 1 到 1000 内的所有完全数
- 循环链表的创建、插入、删除、逆序、显示(C++实现)
- C++中的虚函数
- 第4周 C语言及程序设计提高例程-6 参数传递方式:传值与传地址
- C++ explicit的作用
- Anonymous function in C++
- Atrenta电话面试(C++研发工程师)
- c++ memset、memcpy函数