From: http://z2403030202.blog.163.com/blog/static/40816034200891553750894/
# Include <iostream> // do not use iostream. H. Many problems may occur.
# Include <iomanip> // header file for Io traffic control, mainly for some operations such as SETW (int n), setprecision (int n)
//, Setbase (int n), setfill (char C.
▲Setw (n) usage: in general, it is the preset width.
For example, cout <SETW (5) <255 <Endl;
The result is:
(Space) 255
▲Setfill (char c) usage: Fill in the specified character C if the preset width already has a useless width.
For example, cout <setfill (mailto: % 20 @ % 20) % 3C % 3 csetw (5) % 3C % 3c255% 3C % 3 cendl;
The result is:
@ 255
▲Setbase (int n): converts a number to n-base.
For example, cout <setbase (8) <SETW (5) <255 <Endl;
Cout <setbase (10) <SETW (5) <255 <Endl;
Cout <setbase (16) <255 <Endl;
The result is:
(Space) 377
(Space) 255
(Space) f
▲Setprecision usage
You can use setprecision (n) to control the number of floating point numbers displayed in the output stream. The default value of C ++ is 6.
If setprecision (n) is used with setiosflags (IOs: fixed), you can control the number of digits on the right of the decimal point. Setiosflags (IOs: fixed) is a real number expressed by a fixed point.
If used with setiosnags (IOs: Scientific), you can control the number of decimal places in exponential notation. Setiosflags (IOs: Scientific) is an exponential representation of real numbers.
For example, the following code represents a real number by floating point, fixed point, and exponent respectively:
# Include <iostream. h>
# Include <iomanip. h> // format controller is required
Void main ()
{
Double amount = 22.0/7;
Cout <amount <Endl;
Cout <setprecision (0) <amount <Endl
<Setprecision (1) <amount <Endl
<Setprecision (2) <amount <Endl
<Setprecision (3) <amount <Endl
<Setprecision (4) <amount <Endl;
Cout <setiosflags (IOs: fixed );
Cout <setprecision (8) <amount <Endl;
Cout <setiosflags (IOs: Scientific)
<Amount <Endl;
Cout <setprecision (6); // reset to the original default setting
}
The running result is:
3.14286
3
3
3.1
3.14
3.143
3.14285714
3.14285714e + 00
The program runs on a 32-bit machine.
In the output represented by floating point, setprecision (n) indicates the number of valid digits.
The number of valid digits is not set before the output value of the 1st row. Therefore, the default value of the valid digits of the stream is 6: 0 for the 2nd output, and the minimum valid digits of C ++ are 1, as a result, set the number of valid digits to 1: 3rd ~ 6 rows of output are output according to the specified number of valid digits.
In the output represented by a fixed point, setprecision (n) indicates the number of decimal places.
7th row output is used with setiosflags (IOs: fixed. Therefore, setprecision (8) sets the number of digits after the decimal point, not the total number of digits.
When output in exponential form, setprecision (n) indicates the number of decimal places.
8th row output uses setiosflags (IOs: Scientific) to represent the output form of exponential representation. The effective BITs follows the last set value of 8.
SETW (n) is used to set the domain width.
The number of characters that your output occupies.
For example:
Cout <SETW (5) <12345 <Endl;
Output
12345
Cout <SETW (6) <12345 <Endl;
Output
12345
If the width of the output character exceeds the n value of setw (n), the output is based on the width of the output character.
For example, your cout <SETW (4) <12.3456 <Endl;
Output 12.3456