如何实现中小数点输出的格式化代码示例?
- 内容介绍
- 文章标签
- 相关推荐
本文共计835个文字,预计阅读时间需要4分钟。
在《算法竞赛入门经典》一书中,习题1-5页的打折(折扣)优惠:一件衣服95元,若消费满300元,可打八五折。请输入购买衣服数量,输出需支付金额(保留两位小数)。
在《算法竞赛入门经典》一书中
习题1-5 打折 (discount)
一件衣服95元,若消费满300元,可打八五折。输入购买衣服件数,输出需要支付的金额(单位:元),保留两位小数。
我编写的代码为
#include<iostream> #include<iomanip> using namespace std; int main(void){ double s; cin>>s; if(s*95>=300){ cout<<setiosflags(ios::fixed)<<setprecision(2)<<s*95*0.85<<endl; } else{ cout<<setiosflags(ios::fixed)<<setprecision(2)<<s*95<<endl; } return 0; }
于是将C++中如何显示不同格式的小数查了一下:
Floating point output can be changed with << setiosflags( ios::fixed ) : never use scientific notation //绝对不使用科学记数法。
本文共计835个文字,预计阅读时间需要4分钟。
在《算法竞赛入门经典》一书中,习题1-5页的打折(折扣)优惠:一件衣服95元,若消费满300元,可打八五折。请输入购买衣服数量,输出需支付金额(保留两位小数)。
在《算法竞赛入门经典》一书中
习题1-5 打折 (discount)
一件衣服95元,若消费满300元,可打八五折。输入购买衣服件数,输出需要支付的金额(单位:元),保留两位小数。
我编写的代码为
#include<iostream> #include<iomanip> using namespace std; int main(void){ double s; cin>>s; if(s*95>=300){ cout<<setiosflags(ios::fixed)<<setprecision(2)<<s*95*0.85<<endl; } else{ cout<<setiosflags(ios::fixed)<<setprecision(2)<<s*95<<endl; } return 0; }
于是将C++中如何显示不同格式的小数查了一下:
Floating point output can be changed with << setiosflags( ios::fixed ) : never use scientific notation //绝对不使用科学记数法。

