How to preserve 2-bit decimal output in C + +

Source: Internet
Author: User


How to retain 2 decimal places in C + +.

Cout<<setiosflags (ios::fixed) <<setprecision (2) << variable <<endl;

The above code requires a header file #include <iomanip> .

The Setprecision function refers to the precision of setting the output, for example:

float a=2017.666;
Cout<<setprecision (5) <<a;

2017.7** will be output (note: This will be rounded) * * If the parameter in the setprecision is less than the integer digits, it will be exported exponentially, for example:

float a=123.666;
Cout<<setprecision (2) <<a; Will output 1.2e+002. Setiosflags (ios::fixed) refers to a fixed floating-point display, when the Setiosflags (ios::fixed) and Setprecision (n) two are used together to identify the reserved n for fractional output. It's also important to note that each output is set sequentially, because the two-part scope is the successor, not the one that works on the last object.

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.