SETW, Setprecision, Setfill, and SetBase functions in C + + header file Iomanip.h

Source: Internet
Author: User



#include <iostream>//Do not use iostream.h, there will be a lot of problems #include <iomanip>/IO Flow control header file, mainly some manipulation uses such as SETW (int n), SETPR ecision (int n), setbase (int n), Setfill (char c).

▲SETW (N) Usage: In layman's terms, the preset width
such as COUT&LT;&LT;SETW (5) <<255<<endl;
The result:
Spaces (space) 255
▲setfill (char c) Usage: That is, in the preset width, if an unused width is already present, populate with the character C set
such as Cout<<setfill (' @ ') &LT;&LT;SETW (5) <<255<<endl;
The result:
@@255
▲setbase (int n): Converts a number to n.
such as Cout<<setbase (8) &LT;&LT;SETW (5) <<255<<endl;
Cout<<setbase (&LT;&LT;SETW) (5) <<255<<endl;
Cout<<setbase (<<255<<endl;)
The result:
(space) (space) 377
Spaces (space) 255
Spaces (space) FF
▲setprecision usage
Use Setprecision (n) to control how the output stream displays the number of floating-point numbers. The default stream output value for C + + is 6.
If Setprecision (n) is combined with setiosflags (ios::fixed), you can control the number of digits to the right of the decimal point. Setiosflags (ios::fixed) is a fixed-point representation of a real number.
If combined 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 in floating-point, fixed-point, and exponential mode, respectively:

#include <iostream> #include <iomanip>intMain () {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 original default settings   &NBSP;&NBSP;&NBsp; return 0; }

The results of the operation are:
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) represents the number of significant digits.
Line 1th does not have a significant number of digits set before outputting the value. Therefore, the number of valid digits of the stream is set by default of 6: The 2nd output sets a significant number of digits 0,c++ the minimum number of significant digits is 1, so the number of significant digits is set to one view: line 3rd to 6th output is output by the number of significant digits set.
In the output represented by a fixed-point, Setprecision (n) represents the number of decimal places.
Line 7th output is shared with Setiosflags (ios::fixed). So Setprecision (8) sets the number of digits after the decimal point, not the total number of digits.
When outputting in exponential form, Setprecision (n) represents the number of decimal places.
Line 8th output uses Setiosflags (ios::scientific) to represent the output form of the exponential representation. The number of significant digits follows the last set value of 8.

SETW (n) is the Set field width.
is how many characters your output will take up.
Like what:
COUT<<SETW (5) <<12345<<endl;
On the output:
12345


   COUT<<SETW (6) <<12345<<endl
   output:
   12345
   If you want to output a character width that exceeds the n value of SETW (n), it is output by the width of the output character.
   as your COUT<<SETW (4) <<12.3456<<endl;
   on output 12.3456

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.