C++中小數(shù)點(diǎn)輸出格式(實(shí)例代碼)
在《算法競(jìng)賽入門經(jīng)典》一書中
習(xí)題1-5 打折?。╠iscount)
一件衣服95元,若消費(fèi)滿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 //絕對(duì)不使用科學(xué)記數(shù)法。 << setiosflags( ios::scientific ) : always use scientific notation //使用科學(xué)記數(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ù)點(diǎn)后面的零是否顯示。 << 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ù)點(diǎn)后面的位數(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
請(qǐng)按任意鍵繼續(xù). . .
以上這篇C++中小數(shù)點(diǎn)輸出格式(實(shí)例代碼)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C++簡(jiǎn)明講解缺省參數(shù)與函數(shù)重載的用法
所謂缺省參數(shù),顧名思義,就是在聲明函數(shù)的某個(gè)參數(shù)的時(shí)候?yàn)橹付ㄒ粋€(gè)默認(rèn)值,在調(diào)用該函數(shù)的時(shí)候如果采用該默認(rèn)值,你就無須指定該參數(shù)。C++ 允許多個(gè)函數(shù)擁有相同的名字,只要它們的參數(shù)列表不同就可以,這就是函數(shù)的重載,借助重載,一個(gè)函數(shù)名可以有多種用途2022-06-06
C語言數(shù)據(jù)結(jié)構(gòu)之順序數(shù)組的實(shí)現(xiàn)
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)之順序數(shù)組的實(shí)現(xiàn)的相關(guān)資料,這里提供實(shí)現(xiàn)實(shí)例,希望通過本文能幫助到大家,需要的朋友可以參考下2017-08-08
基于C語言實(shí)現(xiàn)的貪吃蛇游戲完整實(shí)例代碼
這篇文章主要介紹了基于C語言實(shí)現(xiàn)的貪吃蛇游戲完整實(shí)例代碼,對(duì)于學(xué)習(xí)游戲開發(fā)的朋友有一定的借鑒價(jià)值,需要的朋友可以參考下2014-08-08
淺析C++字節(jié)對(duì)齊容易被忽略的兩個(gè)問題
今天我就和大家分享一下C++字節(jié)對(duì)齊容易被忽略的兩個(gè)問題。以下問題也是我實(shí)際開發(fā)工作中遇到的,如果有不同意見歡迎交流2013-07-07
基于C++實(shí)現(xiàn)高精度計(jì)時(shí)器
chrono是C++ 11中的時(shí)間庫,它提供了跨平臺(tái)的高精度時(shí)鐘解決方案,精確到納秒級(jí),本文主要為大家詳細(xì)介紹了如何使用chrono實(shí)現(xiàn)高精度計(jì)時(shí)器,感興趣的可以了解下2024-02-02

