First #include <iomanip>
One: setprecision
Function: Control output stream shows the number of floating-point numbers, Setprecision (n) is the output n number, there will be rounded
Double s=20.7843000;
Cout<<setprecision (1) <<s<<endl; will output 2e+001, because to output a number, so only 2
Cout<<setprecision (2) <<s<<endl; will output 21.
Cout<<setprecision (3) <<s<<endl; output 20.8
Cout<<setprecision (6) <<s<<endl; output 20.7843
Cout<<setprecision (7) <<s<<endl; output 20.7843
Cout<<setprecision (8) <<s<<endl; output 20.7843
Visible, the fractional part at the end of the 0 o'clock, is not output
If you want to output, you have to use Showpoint.
If you add a two statement to the following statements:
cout<<1<<endl;
cout<<1.00800<<endl;
First output: 1. Not a floating-point type
The second article is: 1.008. To undertake the setprecision (8) of this rule statement
If there is a statement directly
int main ()
{
cout<<1<<endl;
cout<<1.00<<endl;
}
First output: 1
The second article is also: 1
II: Setprecision and Showpoint
Declare before output statement: COUT.SETF (ios::showpoint); it's all right.
Double s=20.7843000,
COUT.SETF (Ios::showpoint);
Cout<<setprecision (1) <<s<<endl; will output 2.e+001, notice that there is a "." Between 2 and E.
Cout<<setprecision (2) <<s<<endl; will output 21. Multiple points
Cout<<setprecision (3) <<s<<endl; output 20.8
Cout<<setprecision (6) <<s<<endl; output 20.7843
Cout<<setprecision (7) <<s<<endl; output 20.78430
Cout<<setprecision (8) <<s<<endl; output 20.784300
Visible, the desired number of data will be output
If you add a two statement to the following statements:
cout<<1<<endl;
cout<<1.0080<<endl;
First output: 1 is not a floating-point type.
The second article is also: 1.0080000 to undertake the setprecision (8) of this rule statement.
Three: Setprecision and fixed
If you want to keep a few decimals, the setprecision has to be used with the fixed
Declare before output statement: COUT.SETF (ios::fixed);
Double s=20.7843909
COUT.SETF (ios::fixed);
Cout<<setprecision (1) <<s<<endl; will output 2.8
Cout<<setprecision (2) <<s<<endl; will output 21.78. Multiple points
Cout<<setprecision (3) <<s<<endl; output 20.784
Cout<<setprecision (6) <<s<<endl; output 20.784391
Cout<<setprecision (7) <<s<<endl; output 20.7843909
Cout<<setprecision (8) <<s<<endl; output 20.78439090
If you also add a two statement after these statements:
cout<<1<<endl;
cout<<1.008<<endl;
First output: 1
The second article is: 1.00800000
is to undertake the setprecision (8) of this rule statement, is floating-point type will retain 8 decimal places. Is it an integral type or an integral type?
Statements can also be written as: Cout<<fixed<<setprecision (2) <<s<<endl;
Even if the following statement does not write <<fixed, the same will be treated as <<fixed
Cout<<fixed<<setprecision (2) <<s<<endl;
A:cout<<setprecision (7) <<s<<endl;
B:cout<<setprecision (8) <<s<<endl;
The AB statements are processed in 7 and 8 decimal places, and no more than 7 or 8 floating point numbers are processed.
If you continue to add statement C below:
Cout<<1.008<<endl; will also retain 8 decimal places
Double s=20.7843909
Cout<<setprecision (2) <<s<<endl;//output 21
cout<<fixed<<s<<endl;//Output 20.78
Cout<<setprecision (2) <<s<<endl;//output 21
cout<<showpoint<<s<<endl;//output 21. (There is a point)
cout<<fixed<<s<<endl;//Output 20.78391
cout<<showpoint<<s<<endl;//Output 20.78391
Cout<<setprecision (2) <<s<<endl;//output 21
cout<<fixed<<s<<endl;//Output 20.78
cout<<showpoint<<s<<endl;//Output 20.78
Cout<<setprecision (2) <<s<<endl;//output 21
Cout<<showpoint<<s<<endl;//21. (There is a point)
cout<<fixed<<s<<endl;//20.78
C + + setprecision fixed Showpoint