The use of setiosflags () in C ++, and the use of setiosflags in C ++
Cout <setiosflags (ios: fixed) <setiosflags (ios: right) <setprecision (2 );
Setiosflags is the C ++ operator included in the namespace iomanip. The operator is executed by a specified parameter.
Actions in the region;
Iso: fixed is one of the parameters of the setiosflags operator. The action specified by this parameter represents a floating point with a decimal point.
Number, and move the number to the right of the decimal point as much as possible within the allowed precision range;
Iso: right is also a setiosflags parameter, which specifies the right-aligned output in the specified area;
Setprecision is also a C ++ operator included in the namespace iomanip. This operator is used to set floating point numbers;
Setprecision (2) indicates the precision of the decimal point output, that is, the number of digits on the right of the decimal point is 2.
Cout <setiosflags (ios: fixed) <setiosflags (ios: right) <setprecision (2 );
The combination means to output a floating point number of two digits after the right-aligned decimal point.
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.
Supplement:
Q: What is the role of cout. setf () and cout. precision () in C ++?
A:
These two are format-controlled ~ In the ostream member functions, you can also use the output stream operator to control them, all of which are the same ~ For more information, see ~
Cout. setf is the same as setiosflags, and cout. precision is the same as setprecision ~
# Include <iomanip>
Here, iomanip has many functions:
This mainly applies to some operations such as cin and cout, such as setfill, setw, setbase, and setprecision. It is an I/O flow control header.
File, just like the formatted output in C. The following are some common control functions:
The base number of dec is 10, which is equivalent to "% d"
The base number of hex is 16, which is equivalent to "% X"
The base number of oct is 8, which is equivalent to "% o"
Setfill (c) is set to c
Setprecision (n) is set to show n decimal places
Setw (n): Set the domain width to n characters.
This control operator ensures that the output width is n. For example:
Cout <setw (3) <1 <setw (3) <10 <setw (3) <100; the output result is
1 10100 (right-aligned by default) setw (3) does not work when the output length is greater than 3 (<1000.
Setioflags (ios: fixed) fixed floating point display
Setioflags (ios: scientific) index Representation
Setiosflags (ios: left) left alignment
Setiosflags (ios: right) right-aligned
Setiosflags (ios: skipws ignore leading Blank
Setiosflags (ios: uppercase) hexadecimal number uppercase output
Setiosflags (ios: lowercase) hexadecimal lowercase output
Setiosflags (ios: showpoint) Forcibly displays the decimal point
Setiosflags (ios: showpos) force display symbol
Example:
# Include <iostream>
# Include <iomanip>
Using namespace std;
Int main ()
{
Cout <12345.0 <endl; // output "12345"
Cout <setiosflags (ios: fixed) <setprecision (3) <1.2345 <endl; output "1.235"
Cout <setiosflags (ios: scientific) <12345.0 <endl; // output "1.234500e +
004"
Cout <setprecision (3) <12345.0 <endl; // output "1.235e + 004"
Return 0;
}