C++中IO设置数字精度问题

转载自https://blog.csdn.net/jing_xian/article/details/20239381

/*setprecision函数控制输出流显示浮点数的有效位数
 * 如果和fixed合用的话,控制小数点右面的位数,fixed的意思是从小数点开始计数
*/
#include<iostream>
#include<iomanip> //setprecision函数在这个头文件
using namespace std;
 
int main()
{
cout << setprecision(8) << 10.0/3 << endl;//输出8位有效数字
cout << setprecision(8) << 1.0/3 << endl;
cout << fixed << setprecision(8) << 10.0/13 << endl;//输出到小数点位后八位
cout << fixed << setprecision(8) << 1.0/13 << endl;
return 0;
}

  

参考链接:http://www.cplusplus.com/reference/iomanip/

// setbase example
#include <iostream>     // std::cout, std::endl
#include <iomanip>      // std::setbase
using namespace std;
int main () {
     //参数只能为 16 10 8 cout << setbase(16) << 110 << endl; cout << setbase(10) << 110 << endl; cout << setbase(8) << 110 << endl; //2为无效参数 cout << setbase(2) << 110 << endl; return 0; }

  

 

posted on 2018-09-17 15:15  Virtualmate  阅读(206)  评论(0编辑  收藏  举报

导航