Based on the usage of setiosflags of in C++

  • 2020-05-30 20:49:14
  • OfStack

cout < < setiosflags(ios::fixed) < < setiosflags(ios::right) < < setprecision(2);

setiosflags is the C++ operator contained in the namespace iomanip, which is used to perform actions in a region specified by a parameter.

iso::fixed is one of the parameters of the operator setiosflags, which specifies that the action is to represent a floating point number with a decimal point, and to move the number to the right of the decimal point as far as possible to the allowed precision.

iso::right is also a parameter of setiosflags, whose specified function is to right-align the output in the specified area;

setprecision is also the C++ operator contained in the namespace iomanip, which sets floating point Numbers.

setprecision(2) means the precision of the decimal point output, that is, the number of Numbers to the right of the decimal point is 2.

cout < < setiosflags(ios::fixed) < < setiosflags(ios::right) < < setprecision(2);

To combine in 1 means to output a floating point number, two decimal places to the right.

Use setprecision(n) to control the number of floating point Numbers displayed in the output stream. C++ the default stream output value is 6 valid bits.

If setprecision(n) is combined with setiosflags(ios::fixed), you can control the number of Numbers to the right of the decimal point.

setiosflags(ios::fixed) is a fixed-point representation of real Numbers


Q: what is the effect of cout.setf () and cout.precision () in C++?

A: these two are in the format control ~ostream member functions, which can also be controlled by the output stream operator. Please check them out. cout.setf and setiosflags1, cout.precision and setprecision1
#include < iomanip >

Here we iomanip role more: mainly for cin, cout 1 some manipulation of the transport operator, such as such as setfill setw, setbase, setprecision and so on. It is the I/O flow control header file, just like the formatted output 1 in C.

dec with a cardinality of 10 is equivalent to "%d"
hex with a cardinality of 16 is equivalent to "%X"
oct with a cardinality of 8 is equivalent to "%o"
setfill(c) set the fill character to c
setprecision(n) set the display decimal precision to n bits
setw(n) set the field width to n characters
This control means that the output width is guaranteed to be n. Such as:
cout < < setw(3) < < 1 < < setw(3) < < 10 < < setw(3) < < 100; The output result is
1 10100 (right-aligned by default) when the output length is greater than 3 ( < < 1000), setw(3) doesn't work.
setioflags(ios::fixed) fixed floating point display
setioflags(ios::scientific) index
setiosflags (ios: : left) left alignment
setiosflags (ios: : right) right alignment
setiosflags(ios::uppercase) hexadecimal number uppercase output
setiosflags(ios::lowercase) hexadecimal lowercase output
setiosflags(ios::showpoint) forces the decimal point to be displayed
setiosflags(ios::showpos) forces the symbol to be displayed

For example:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
cout<<12345.0<<endl;// The output "12345"
cout<<setiosflags(ios::fixed)<<setprecision(3)<<1.2345<<endl; The output "1.235"
cout<<setiosflags(ios::scientific)<<12345.0<<endl;// The output "1.234500e+
004 "
cout<<setprecision(3)<<12345.0<<endl;// The output "1.235e+004 "
return 0;

Related articles: