C++中小数点输出格式(实例代码)

发布时间:2020-10-18 22:13:58 作者:jingxian
来源:脚本之家 阅读:192

在《算法竞赛入门经典》一书中

习题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 //绝对不使用科学记数法。
<< setiosflags( ios::scientific ) : always use scientific notation //使用科学记数法。
<< resetiosflags( ios::floatfield ) : restores default (use fixed or scientific notation based on the precision)//重置为缺省。
<< (re)setiosflags( ios::showpoint ) : controls if the trailing zeros and decimal point will be output (default is no) //控制小数点后面的零是否显示。
<< setprecision( digits ) : if fixed or scientific format is selected, controls the number of digits after the decimal place, otherwise controls the total number of significant digits// 如果fixed或者scientific已经确定了,则控制小数点后面的位数;否则,控制所有位数。
#include <iostream>
#include <iomanip>
using namespace std;

int main(void){
  double S = 0.000000123;
  double A = 456.7;
  double B = 8910000000000.0;

  cout << "default precision is "
     << cout.precision() << endl;  // 6

  cout << "  S = " << S << endl;  // 1.23e-07
  cout << "  A = " << A << endl;  // 456.7
  cout << "  B = " << B << endl;  // 8.91e+12

  cout << setiosflags(ios::showpoint)
     << "ios::showpoint ON" << endl;

  cout << "  S = " << S << endl;  // 1.23000e-07
  cout << "  A = " << A << endl;  // 456.700
  cout << "  B = " << B << endl;  // 8.91000e+12

  cout << resetiosflags(ios::showpoint)
     << "ios::showpoint OFF" << endl;
  cout << setiosflags(ios::fixed)
     << "ios::fixed ON" << endl;

  cout << "  S = " << S << endl;  // 0.000000
  cout << "  A = " << A << endl;  // 456.700000
  cout << "  B = " << B << endl;  // 8910000000000.000000

  cout << resetiosflags(ios::fixed)
     << setiosflags(ios::scientific)
     << "ios::scientific ON" << endl;

  cout << "  S = " << S << endl;  // 1.230000e-07
  cout << "  A = " << A << endl;  // 4.567000e+02
  cout << "  B = " << B << endl;  // 8.910000e+12
}

输出为:

default precision is 6
S = 1.23e-007
A = 456.7
B = 8.91e+012
ios::showpoint ON
S = 1.23000e-007
A = 456.700
B = 8.91000e+012
ios::showpoint OFF
ios::fixed ON
S = 0.000000
A = 456.700000
B = 8910000000000.000000
ios::scientific ON
S = 1.230000e-007
A = 4.567000e+002
B = 8.910000e+012

--------------------------------

Process exited after 0.02482 seconds with return value 0

请按任意键继续. . .

以上这篇C++中小数点输出格式(实例代码)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。

推荐阅读:
  1. 用面向对象思想实现时钟C++描述
  2. C++小知识:不要节约代码行数

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++ 输出格式 小数点

上一篇:Android编程视频播放API之MediaPlayer用法示例

下一篇:解决异常FileNotFoundException:class path resource找不到资源文件的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》