C++中小數(shù)點輸出格式(實例代碼)
在《算法競賽入門經(jīng)典》一書中
習題1-5 打折?。╠iscount)
一件衣服95元,若消費滿300元,可打八五折。輸入購買衣服件數(shù),輸出需要支付的金額(單位:元),保留兩位小數(shù)。
我編寫的代碼為
#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++中如何顯示不同格式的小數(shù)查了一下:
Floating point output can be changed with << setiosflags( ios::fixed ) : never use scientific notation //絕對不使用科學記數(shù)法。 << setiosflags( ios::scientific ) : always use scientific notation //使用科學記數(shù)法。 << 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) //控制小數(shù)點后面的零是否顯示。 << 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已經(jīng)確定了,則控制小數(shù)點后面的位數(shù);否則,控制所有位數(shù)。
#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
請按任意鍵繼續(xù). . .
以上這篇C++中小數(shù)點輸出格式(實例代碼)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持我們。
上一篇:C程序中唯一序列號的生成實例詳解
欄 目:C語言
下一篇:數(shù)據(jù)結(jié)構(gòu)用兩個棧實現(xiàn)一個隊列的實例
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1441.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10APUE筆記之:進程環(huán)境詳解
- 01-10c++中inline的用法分析
- 01-10如何尋找數(shù)組中的第二大數(shù)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10delphi制作wav文件的方法
- 01-11ajax實現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什