C++ decimal point output format of instance code
- 2020-05-24 05:55:31
- OfStack
In the classic introduction to algorithmic racing 1
Problem sets 1-5 discount (discount)
B: it's 95 yuan per dress. You can get a 15% discount if you spend more than 300 yuan. Input the number of pieces of clothes, output the amount to be paid (unit: yuan), and keep two decimal places.
The code I wrote is
#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;
}
So I looked up how to display decimals in different formats in C++ :
Floating point output can be changed with
<< setiosflags( ios::fixed ) : never use scientific notation // Never use scientific notation.
<< setiosflags( ios::scientific ) : always use scientific notation // Use scientific notation.
<< resetiosflags( ios::floatfield ) : restores default (use fixed or scientific notation based on the precision)// Reset to the default.
<< (re)setiosflags( ios::showpoint ) : controls if the trailing zeros and decimal point will be output (default is no) // Controls whether the zero after the decimal point is displayed.
<< 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// if fixed or scientific Once determined, control the number of digits behind the decimal point; Otherwise, control all digits.
#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
}
The output is:
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
Please press any key to continue..