SETW, setprecision, setfill, and setbase functions in the 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 stream control header file, mainly for some operations such as SETW (int n), setprecision (int n ), setbase (int n), setfill (char C.

SETW (n) usage: Generally, it is the preset width.
For example, cout <SETW (5) <255 <Endl;
The result is:
(Space) 255
Setfill (char c) usage: Fill in the specified character C if the preset width already has a useless width.
For example, cout <setfill ('@') <SETW (5) <255 <Endl;
The result is:
@ 255
Setbase (int n): converts a number to n-base.
For example, cout <setbase (8) <SETW (5) <255 <Endl;
Cout <setbase (10) <SETW (5) <255 <Endl;
Cout <setbase (16) <255 <Endl;
The result is:
(Space) 377
(Space) 255
(Space) FF
Setprecision usage
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.
If used 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 by floating point, fixed point, and exponent respectively:

# Include <iostream> # include <iomanip> int main () {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 the original default value return 0 ;}

The running result is:
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) indicates the number of valid digits.
The number of valid digits is not set before the output value of the 1st row. Therefore, the default value of the valid digits of the stream is 6: 0 for the 2nd output, and the minimum valid digits of C ++ are 1, as a result, set the number of valid digits to 1: 3rd ~ 6 rows of output are output according to the specified number of valid digits.
In the output represented by a fixed point, setprecision (n) indicates the number of decimal places.
7th row output is used with setiosflags (IOs: fixed. Therefore, setprecision (8) sets the number of digits after the decimal point, not the total number of digits.
When output in exponential form, setprecision (n) indicates the number of decimal places.
8th row output uses setiosflags (IOs: Scientific) to represent the output form of exponential representation. The valid BITs follows the last set value of 8.

SETW (n) is used to set the domain width.
The number of characters in your output.
For example:
Cout <SETW (5) <12345 <Endl;
Output:
12345

Cout <SETW (6) <12345 <Endl;
Output:
12345
If the width of the output character exceeds the n value of setw (n), the output is based on the width of the output character.
For example, your cout <SETW (4) <12.3456 <Endl;
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.