C++中小數(shù)點(diǎn)輸出格式(實(shí)例代碼)
在《算法競(jìng)賽入門經(jīng)典》一書中
習(xí)題1-5 打折 (discount)
一件衣服95元,若消費(fèi)滿300元,可打八五折。輸入購(gòu)買衣服件數(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讀txt到二維數(shù)組的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇C讀txt到二維數(shù)組的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
C++實(shí)現(xiàn)線程池的簡(jiǎn)單方法示例
這篇文章主要給大家介紹了關(guān)于C++實(shí)現(xiàn)線程池的簡(jiǎn)單方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C++具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
C/C++百行代碼實(shí)現(xiàn)熱門游戲消消樂(lè)功能的示例代碼
這篇文章主要介紹了C/C++百行代碼實(shí)現(xiàn)熱門游戲消消樂(lè)功能的示例代碼,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
C語(yǔ)言中函數(shù)棧幀的創(chuàng)建和銷毀的深層分析
在C語(yǔ)言中,每一個(gè)正在運(yùn)行的函數(shù)都有一個(gè)棧幀與其對(duì)應(yīng),棧幀中存儲(chǔ)的是該函數(shù)的返回地址和局部變量。從邏輯上講,棧幀就是一個(gè)函數(shù)執(zhí)行的環(huán)境:函數(shù)參數(shù)、函數(shù)的局部變量、函數(shù)執(zhí)行完后返回到哪里等等2022-04-04
Qt實(shí)現(xiàn)拖動(dòng)單個(gè)控件移動(dòng)的示例代碼
做慣了靜態(tài)圖,今天來(lái)搞一搞動(dòng)態(tài)圖吧!本文將利用Qt實(shí)現(xiàn)拖動(dòng)單個(gè)控件移動(dòng)效果,文中的示例代碼講解詳細(xì),感興趣的可以動(dòng)手嘗試一下2022-06-06
c++ qsort 與sort 對(duì)結(jié)構(gòu)體排序?qū)嵗a
這篇文章主要介紹了c++ qsort 與sort 對(duì)結(jié)構(gòu)體排序?qū)嵗a,幫助大家更好的理解和學(xué)習(xí)c++,感興趣的朋友可以了解下2020-11-11
C語(yǔ)言統(tǒng)計(jì)字符個(gè)數(shù)代碼分享
本文給大家分享的是2則C語(yǔ)言實(shí)現(xiàn)統(tǒng)計(jì)字符個(gè)數(shù)的代碼,非常的簡(jiǎn)單實(shí)用,小伙伴們根據(jù)自己的項(xiàng)目需求自由選擇吧。2015-07-07

