国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++中小數點輸出格式(實例代碼)

2020-01-26 14:04:37
字體:
來源:轉載
供稿:網友

在《算法競賽入門經典》一書中

習題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++中小數點輸出格式(實例代碼)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 疏勒县| 紫阳县| 延长县| 萨嘎县| 新巴尔虎右旗| 南华县| 宕昌县| 石河子市| 防城港市| 阿荣旗| 塔河县| 崇义县| 府谷县| 枣强县| 元江| 莫力| 康定县| 霸州市| 平遥县| 浮山县| 同心县| 万宁市| 宁海县| 怀远县| 红桥区| 广宁县| 塘沽区| 德庆县| 西林县| 余江县| 浦县| 耿马| 五寨县| 法库县| 阜新市| 巴里| 长治市| 奇台县| 河曲县| 罗山县| 荣昌县|