Setprecision, fixed, and showpoint usage summary, setprecisionfixed

Source: Internet
Author: User

Setprecision, fixed, and showpoint usage summary, setprecisionfixed

First, add the header file: iomanip

I. setprecision

Purpose: control the number of floating-point numbers displayed in the output stream. setprecision (n) is the number of n output values, which are rounded in.

For example, double s = 20.7843000,

Cout <setprecision (1) <s <endl; outputs 2e + 001. Because a number is output, only 2.

Cout <setprecision (2) <s <endl; outputs 21.

Cout <setprecision (3) <s <endl; will output 20.8.

Cout <setprecision (6) <s <endl; will output 20.7843.

Cout <setprecision (7) <s <endl; will output 20.7843.

Cout <setprecision (8) <s <endl; will output 20.7843.

It can be seen that when the end of the decimal part is 0, it cannot be lost!

Showpoint is required for output.

Note:

(If two statements are added after these statements:

Cout <1 <endl;

Cout <1.00800 <endl;

Can I guess what will be output?

Output 1: 1. It is not a float type.

Article 2: 1.008. Take the rule Statement of setprecision (8.

Note:

If a statement exists directly

Int main ()

{

Cout <1 <endl;

Cout <1.00 <endl;

}

Output 1: 1.

The second entry is also: 1. Output by integer
)

Ii. setprecision and showpoint

Syntax: declare cout. setf (ios: showpoint) before the output statement; that's all!

For example, double s = 20.7843000,

Cout. setf (ios: showpoint );

Cout <setprecision (1) <s <endl; will output 2.e+ 001. Note that a "." is added between 2 and e.

Cout <setprecision (2) <s <endl; outputs 21 .. Multiple points!

Cout <setprecision (3) <s <endl; will output 20.8.

Cout <setprecision (6) <s <endl; will output 20.7843.

Cout <setprecision (7) <s <endl; will output 20.78430.

Cout <setprecision (8) <s <endl; will output 20.784300.

As you can see, the desired data quantity will be output!

Note:

(If two statements are added after these statements:

Cout <1 <endl;

Cout <1.0080 <endl;

Can I guess what will be output?

Output 1: 1. It is not a float type.

The second is 1.0080000. Take the rule Statement of setprecision (8.

 

Iii. setprecision and fixed

If you want to keep a few decimal places, setprecision has to work with fixed !!

Syntax: declare cout. setf (ios: fixed) before the output statement );

For example, double s = 20.7843909

Cout. setf (ios: fixed );

Cout <setprecision (1) <s <endl; will output 2.8.

Cout <setprecision (2) <s <endl; will output 21.78. Multiple points!

Cout <setprecision (3) <s <endl; will output 20.784.

Cout <setprecision (6) <s <endl; will output 20.784391.

Cout <setprecision (7) <s <endl; will output 20.7843909.

Cout <setprecision (8) <s <endl; will output 20.78439090.

Note:

(If you add two statements after these statements:

Cout <1 <endl;

Cout <1.008 <endl;

Can I guess what will be output?

Output 1: 1.

Article 2: 1.00800000.

It is the rule statement that undertakes setprecision (8). If it is float, 8 decimal places will be retained. Is it an integer or an integer !)

The statement can also be written as: cout <fixed <setprecision (2) <s <endl;

Even if the following statement is not written <fixed, it will be processed as <fixed.

For example, statements:

Cout <fixed <setprecision (2) <s <endl;

A: cout <setprecision (7) <s <endl;

B: cout <setprecision (8) <s <endl;

All AB statements are processed with 7 reserved digits and 8 decimal digits, instead of 7 or 8 floating point numbers.

If the following statement is c:

Cout <1.008 <endl; also retains 8 decimal places.

Iv. setprecision, showpoint, and fixed

{

Cout <fixed <setprecision (2) <123.456 <endl; // The output result is 123.46.
Cout <showpoint <12345.0006666 <endl; // output 12345.0
Cout <fixed <setprecision (2) <123.456 <endl;

}

For example, double s = 20.7843909

1. statements available

Cout <setprecision (2) <s <endl; // Output 21

Cout <fixed <s <endl; // output 20.78

2. Statements:

Cout <setprecision (2) <s <endl; // Output 21

Cout <showpoint <s <endl; // Output 21. (There is a point)

3. Statements:

Cout <fixed <s <endl; // output 20.78391
Cout <showpoint <s <endl; // output 20.78391
4. Statements:

Cout <setprecision (2) <s <endl; // Output 21
Cout <fixed <s <endl; // output 20.78
Cout <showpoint <s <endl; // output 20.78

5. Statements:

Cout <setprecision (2) <s <endl; // Output 21
Cout <showpoint <s <endl; // 21. (There is a point)
Cout <fixed <s <endl; // 20.78

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.