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

  

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

  

习题1 - 5打折(折扣)

  

一件衣服95元,若消费满300元,可打八五折。输入购买衣服件数、输出需要支付的金额(单位:元),保留两位小数。
  

  

        # include   # include   使用名称空间性病;   int主要(空白){   双s;   ,cin>的在年代;      如果(s * 95祝辞=300){   cout<& lt; setiosflags (ios::固定)& lt; & lt;设置精度(2)& lt; & lt; s * 95 * 0.85 & lt; & lt; endl;   }      其他{   cout<& lt; setiosflags (ios::固定)& lt; & lt;设置精度(2)& lt; & lt; s * 95 & lt; & lt; endl;   }         返回0;   }      

        浮点输出可以改变   & lt; & lt;setiosflags (ios::固定):不要使用科学记数法//绝对不使用科学记数法。   & lt; & lt;setiosflags (ios:科学):总是使用科学记数法//使用科学记数法。   & lt; & lt;resetiosflags (ios::实域):恢复默认(使用基于固定或科学记数法>   # include & lt; iostream>   # include & lt; iomanip>   使用名称空间性病;      int主要(空白){   双S=0.000000123;   双A=456.7;   双B=8910000000000.0;      cout & lt; & lt;“默认精度”   & lt; & lt;cout.precision () & lt; & lt;endl;//6      cout & lt; & lt;“S=" & lt; & lt;年代& lt; & lt;endl;//1.23 e-07   cout & lt; & lt;“=? lt; & lt;& lt; & lt;endl;//456.7   cout & lt; & lt;“B=" & lt; & lt;B & lt; & lt;endl;//8.91 e + 12      cout & lt; & lt;setiosflags (ios:: showpoint)   & lt; & lt;“ios:: showpoint”& lt; & lt;endl;      cout & lt; & lt;“S=" & lt; & lt;年代& lt; & lt;endl;//1.23000 e-07   cout & lt; & lt;“=? lt; & lt;& lt; & lt;endl;//456.700   cout & lt; & lt;“B=" & lt; & lt;B & lt; & lt;endl;//8.91000 e + 12      cout & lt; & lt;resetiosflags (ios:: showpoint)   & lt; & lt;“ios:: showpoint”& lt; & lt;endl;   cout & lt; & lt;setiosflags (ios::固定)   & lt; & lt;“ios::固定在”& lt; & lt;endl;      cout & lt; & lt;“S=" & lt; & lt;年代& lt; & lt;endl;//0.000000   cout & lt; & lt;“=? lt; & lt;& lt; & lt;endl;//456.700000   cout & lt; & lt;“B=" & lt; & lt;B & lt; & lt;endl;//8910000000000.000000      cout & lt; & lt;resetiosflags (ios::固定)   & lt; & lt;setiosflags (ios::科学)   & lt; & lt;“ios:科学”& lt; & lt;endl;      cout & lt; & lt;“S=" & lt; & lt;年代& lt; & lt;endl;//1.230000 e-07   cout & lt; & lt;“=? lt; & lt;& lt; & lt;endl;//4.567000 e + 02   cout & lt; & lt;“B=" & lt; & lt;B & lt; & lt;endl;//8.910000 e + 12   }      

  

默认精度是6
  S=1.23 e - 007
  一个=456.7
  B=8.91 e + 012
  ios:: showpoint
  S=1.23000 e - 007
  一个=456.700
  B=8.91000 e + 012
  ios:: showpoint
  ios::固定在
  S=0.000000
  一个=456.700000
  B=8910000000000.000000
  ios:科学在
  S=1.230000 e - 007
  002=4.567000 e +
  B=8.910000 e + 012

  

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

  

0.02482秒后退出过程与返回值0

  

请按任意键继续。

  

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

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