C++输出控制小数点后位数的方法


以C++输出小数点两位数为例

setprecision(n)使用方法总结

首先要记住写头文件

	#include <iomanip>  //不要忘了头文件

以下是控制小数位数的三种写法

	//the first
	cout<<setiosflags(ios::fixed)<<setprecision(2);
	//the second
	cout.setf(ios::fixed);
	cout<<setprecision(2);
	//the third
	cout<<fixed<<setprecision(2);
  • 想要保留几位小数setprecision(n)中n就写成几
  • 一定要记得写头文件##

猜你喜欢

转载自blog.csdn.net/weixin_46353366/article/details/106307514