Each output stream has a precision member function, and once the precision function is called for an output stream, a number with a decimal point is printed to the stream, either a total of two digits are retained or two bits are retained after the decimal point, which is determined by the compiler. The precision function only takes effect on the specified stream.
The SETF member function is the abbreviation for set flags, not the abbreviation for set format! It is an instruction to do one thing in the way of two election.
The ios::fixed flag causes the stream to use fixed-point counting to output a number of type double instead of using the E notation
The IOS::SHOWPOINT flag requires that a stream always contain a decimal point in a floating-point number.
The IOS::SCIENTIFIC flag will output numbers in e notation.
Ios::showpos flag is output + number before positive integer
Ios::right flag if used and width is used to specify the width of the field, the next item of the output will be aligned to the right. Default for this item
Ios::left flag if used with width specified field width, the next item of the output will be left-justified
The width member function is used to set the field width. For example:
cout << "a"; Cout.width (4); cout << 7 << Endl;
Then output a 7. There are 3 spaces between a and 7 because the system defaults to ios::right right alignment.
Note that the width member function only adapts to the next item to be output, and if you want to output more than one item, to invoke multiple width, you can use the stream manipulation meta SETW if you are too troublesome.
Any flags you set can be de-set by calling the UNSETF member function to cancel the setting, for example:
COUT.UNSETF (Ios::showpos);
You can cancel a positive integer before displaying the + sign.
SETW and setprecision manipulation elements.
A manipulation element is a function that is called in a non-traditional way. The manipulator is positioned after the insert operator <<. Two manipulation elements are in the Iomanip library, and you need to call the library if you want to use one.
The SETW manipulation element, like the width member function, requires multiple SETW manipulation elements to be called multiple times to set the domain width. In fact, the SETW manipulation element is called the width member function. The following code:
cout << "A" << set (5) << 1 << Set (5) << 2 << set (7) << 3 << Endl;
The output of this statement is: a 1 2 3. There are 4 spaces in front of 1, 2, and 6 spaces in front of 3.
The setprecision manipulation element is the same as the precision member function. The following code:
COUT.SETF (ios::fixed); COUT.SETF (Ios::showpoint); cout << "A" << setprecision (2) << 1.2 << 2.3 << Endl;
The setprecision manipulation element, like the precision member function, is set to take effect. The above example outputs: 1.20 2.30.
This article is from the "_ Conan conan_" blog, please be sure to keep this source http://goodhx.blog.51cto.com/9727085/1734081
Formatted output of C + +