How C + + preserves two-bit decimals and significant digits

Source: Internet
Author: User

Cout<<setiosflags (iOS::fixed) <<setprecision (2);// requires header file # include <iomanip>

Then output the real type variable that can retain 2 decimal places output, of course you want to keep three decimal places, Setprecision (3) on the line.

Setprecision refers to setting the output precision when there is no

Cout<<setiosflags (iOS::fixed)

, the output format is a valid number of digits for the data, such as

float 123.666; cout<<setprecision (5) <<a;

Will output 123.67 (note here that it will be rounded up). If the parameters in the setprecision are less than the integer digits, they will be output in exponential form, for example

float 123.666; cout<<setprecision (2) <<a;

Will output 1.2e+002.

Setiosflags (ios::fixed) refers to a fixed floating-point display that retains n-bit output when setiosflags (ios::fixed) and Serprecision (n) two. It is also important to note that each output is set only once, because the two scopes are subsequent objects, not just the latter.

How C + + preserves two-bit decimals and significant digits

Related Article

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.