fixed setprecision(int n)的使用

#include<iostream>
#include<iomanip> //fixed setprecision() 的库函数

using namespace std;

int main()
{
	double a, b;
	a = 1234567;
	b = 0.1234567;
	//cout << a << " " << b << " " << c << endl;
	//cout << setprecision(2) << a << " " << setprecision(2) << b << " " << setprecision(2) << c << endl;
	//cout << fixed << a << " " << fixed << b << " " << fixed << c << endl;
	cout << fixed << setprecision(2) << a << " " << fixed << setprecision(2) << b << " " << fixed << setprecision(2) << c << endl;
	return 0;
}

无fixed和setprecision:

由此可知,double 类型的数字输出默认为科学计数法,的最大长度为6;大于6位的以四舍五入的形式输出。

无fixed:

setprecision()只用于调整最大长度,计数法依旧为科学计数法。

无setprecision():

 

输出为非科学计数法的值。小数位长度为6。

fixed与setprecision配合使用:

总结来说。

fixed用于取消科学计数法,以正常形式输出;

setprecision()用于浮点数的小数点个数。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值