c++保留小数
Posted 头号理想
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++保留小数相关的知识,希望对你有一定的参考价值。
c++中保留小数的方法
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
#include<iomanip>//必要的头文件
using namespace std;
int main()
double a = 0.123654789;
cout << setiosflags(ios::fixed) << setprecision(3) << a << endl;
//保留三位小数
cout.setf(ios::fixed);
cout << setprecision(2);
cout << a << endl;
//保留两位小数
//第三种方法:也是我最常用的方法
cout << fixed << setprecision(4);
cout << a << endl;
//保留四小数
return 0;
以上是关于c++保留小数的主要内容,如果未能解决你的问题,请参考以下文章