Setprecision
来源:互联网 发布:网络执法官破解版win7 编辑:程序博客网 时间:2024/06/08 02:58
按有效位输出是 setprecision,按小数位数输出也是setprecision,但到底是谁取决于fixed。
cout << resetiosflags(ios::fixed) << setprecision(n) << float-point-number; 是按n位有效数输出
cout << setiosflags(ios::fixed) << setprecision(n) << float-point-number; 是按n位小数输出
测试代码:
#include <iostream>
#include <iomanip>
using namespace std;
int main( void )
{
const double value = 12.3456789;
cout << value << endl; // 默认以6精度,所以输出为 12.3457
cout << setprecision(4) << value << endl; // 改成4精度,所以输出为12.35
cout << setprecision(8) << value << endl; // 改成8精度,所以输出为12.345679
cout << fixed << setprecision(4) << value << endl; // 加了fixed意味着是固定点方式显示,所以这里的精度指的是小数位,输出为12.3457
cout << value << endl; // fixed和setprecision的作用还在,依然显示12.3457
cout.unsetf( ios::fixed ); // 去掉了fixed,所以精度恢复成整个数值的有效位数,显示为12.35
cout << value << endl;
cout.precision( 6 ); // 恢复成原来的样子,输出为12.3457
cout << value << endl;
}
cout << resetiosflags(ios::fixed) << setprecision(n) << float-point-number; 是按n位有效数输出
cout << setiosflags(ios::fixed) << setprecision(n) << float-point-number; 是按n位小数输出
测试代码:
#include <iostream>
#include <iomanip>
using namespace std;
int main( void )
{
const double value = 12.3456789;
cout << value << endl; // 默认以6精度,所以输出为 12.3457
cout << setprecision(4) << value << endl; // 改成4精度,所以输出为12.35
cout << setprecision(8) << value << endl; // 改成8精度,所以输出为12.345679
cout << fixed << setprecision(4) << value << endl; // 加了fixed意味着是固定点方式显示,所以这里的精度指的是小数位,输出为12.3457
cout << value << endl; // fixed和setprecision的作用还在,依然显示12.3457
cout.unsetf( ios::fixed ); // 去掉了fixed,所以精度恢复成整个数值的有效位数,显示为12.35
cout << value << endl;
cout.precision( 6 ); // 恢复成原来的样子,输出为12.3457
cout << value << endl;
}
- Setprecision
- setprecision
- setprecision
- [c++]setprecision()
- setprecision()用法
- setprecision函数
- setprecision()与setiosflags()
- setprecision和setiosflags函数
- setfill、setw 和 setprecision
- setfill、setw 和 setprecision
- setprecision后是要四舍五入吗?
- C++例子:setprecision
- fixed<<setprecision(n)
- 固定小数位数 setprecision()
- 关于setprecision(n)的问题
- setprecision 和 setiosflags(ios::fixed)
- fixed 和setprecision()的用法
- fixed 和setprecision()的用法
- 在非MFC程序中引用CString
- XMLHttpRequest对象
- 基于对话框的程序关闭不了解答
- 合理并灵活使用---宏定义
- Zigbee无线传感器网络平台的设计与实现
- Setprecision
- ARM7——LPC2xxx小总结
- (转帖)小菜编程成长记(十 会修电脑不会修收音机?——聊设计模式原则)
- 为对话框中的控件增加提示的简单方法
- maven入门--概念与实例
- MFC学习(读MFC深入浅出)
- (转帖)小菜编程成长记(十一 三层架构,分层开发)
- 基于ZigBee的无线串口集线器的研究与设计
- VSS+Vs2003使用手册