Finishing C ++ format (section)

First formatted output need to include two headers
 

#include<iostream>
#include<iomanip>

1. three decimal places


    cout<<fixed<<setprecision(3)<<num<<endl;

Note that if something like write:

    cout<<setprecision(3)<<num<<endl;

Reservations are three significant digits of this number num
 

2. Align

Left:

cout<<setiosflags(ios::left)<<num<<endl;

Align Right:

cout<<setiosflags(ios::right)<<num<<endl;

3. character filled

cout<<setfill('0')<<num<<endl;

Note setfill inside the parameters of only a character and can not be written as 0 (number 0 is recognized as a), not a string

(Of course, like so written above have neither art nor sense, you can own a first cout '0' out)

With SET (n) is more sweet food, the following code inserted before the n spaces "following character string"

cout<<setfill(' ')<<setw(n)<<"后续的字符串"<<endl;

 

The above operations are all located in a single statement:

// The day this argument right-justified, the date may be in the single digits, then supplement into two digits, ie 1 -> "01":

cout<<setw(2)<<setfill('0')<<setiosflags(ios::right)<<day;

Subsequent use even more .......

 

 

 

Guess you like

Origin blog.csdn.net/weixin_41956151/article/details/89488639