setprecision, fixed, Showpoint usage summary (classic!!) Super Classic!! )

Source: Internet
Author: User
Tags setf

First you need to add a header file: Iomanip

One: setprecision

Function: Control the number of floating-point numbers displayed by the output stream, Setprecision (n) is the number of n output, there will be rounding.

For example: Double s=20.7843000,

Cout<<setprecision (1) <<s<<endl; will output 2e+001, because there is a number to output, so only 2.

Cout<<setprecision (2) <<s<<endl; will output 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.

Visible, the fractional part of the end of 0 o'clock, can not be lost.

If you want to output, you have to use Showpoint.

Special tips:

(If you then add a two statement after these statements:

cout<<1<<endl;

cout<<1.00800<<endl;

Guess what it will output.

First output: 1. is not a floating-point type.

The second article is: 1.008. Take this rule statement for Setprecision (8).

Note:

If a statement is directly

int main ()

{

cout<<1<<endl;

cout<<1.00<<endl;

}

First output: 1.

The second article is also: 1. Output by integral type

II: Setprecision and Showpoint

Syntax: Declaring before the output statement: COUT.SETF (ios::showpoint); it's OK.

Also for example: Double s=20.7843000,

COUT.SETF (Ios::showpoint);

Cout<<setprecision (1) <<s<<endl; will output 2.e+001, note that 2 and e more than one "."

Cout<<setprecision (2) <<s<<endl; will output 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.

Visible, the number of data you want is output.

Special tips:

(If you then add a two statement after these statements:

cout<<1<<endl;

cout<<1.0080<<endl;

Guess what it will output.

First output: 1. is not a floating-point type.

The second article is also: 1.0080000. Take this rule statement for Setprecision (8).

Three: Setprecision and fixed

If you want to keep a few decimal places, then Setprecision will have to work with fixed.

Syntax: Declaring before the output statement: COUT.SETF (ios::fixed);

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.

Special tips:

(If you also add a two statement after these statements:

cout<<1<<endl;

cout<<1.008<<endl;

Guess what it will output.

First output: 1.

The second article is: 1.00800000.

is to take the setprecision (8) of this rule statement, is a floating-point type will retain 8 decimal places. is an integral type or an integral type. )

Statements can also be written as: Cout<<fixed<<setprecision (2) <<s<<endl;

Even if the following statement does not write <<fixed, the same will be handled according to <<fixed.

For example, there are statements:

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

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

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

The AB statements are processed in 7, 8 decimal places, and no more than 7 or 8 floating-point numbers are processed.

If you have the following statement C:

Cout<<1.008<<endl; will also retain 8 decimal places.

Four: Setprecision, showpoint and fixed

{cout<<fixed<<setprecision (2) <<123.456<<endl;//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. There are statements

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

cout<<fixed<<s<<endl;//Output 20.78

2. There are statements:

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

cout<<showpoint<<s<<endl;//output 21. (one point)

3. There are statements:

cout<<fixed<<s<<endl;//Output 20.78391
cout<<showpoint<<s<<endl;//Output 20.78391
4. There are statements:

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

5. There are statements:

Cout<<setprecision (2) <<s<<endl;//output 21
Cout<<showpoint<<s<<endl;//21. (There's 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.