Decimal point output format in C ++.
Exercise 1-5 discounts (discount) in algorithm competition getting started classic)
95 yuan for a piece of clothing. If you spend 300 yuan, you can get off. Enter the number of purchased clothes, and the amount to be paid (unit: yuan). retain 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 checked 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 value. <(Re) setiosflags (ios: showpoint): controls if the trailing zeros and decimal point will be output (default is no) // controls whether 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 has been determined, the number of digits after the decimal point is controlled; otherwise, all digits are controlled.
#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}
Output:
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.23366e-007
A = 4.567000e + 002
B = 8.9425e + 012
--------------------------------
Process exited after 0.02482 seconds with return value 0
Press any key to continue...
Reference
Https://www.cs.duke.edu/courses/cps149s/fall99/resources/n2.html