Output format for C + +

Source: Internet
Author: User

0 in the C language is very simple to the output requirements, but in C + + has a hint of trouble.

What you need in the following code is

#include <iostream> basic input/Output library

#include <iomanip> input/output operation repertoires

1. General operators

There are three types of general purpose operators:

(1) line break (Endl)

"Endl" is the same effect as the C-language "\ n".

(2) Set width (SETW)

Note Set the width minimum space. If the data requires more space in the output, then cout overrides the request to set the width, using whatever amount of space is required.

There are two alignment options: left-justified and right-aligned.

Right alignment: The data is on the right and the fill character is on the left.

Left: The data is on the left and the fill character is on the right.

    

1#include <iostream>2#include <iomanip>3 using namespacestd;4 5 intMain ()6 {7     intA=123;8     CharC='A';9     Tencout<<a One<<c<<Endl; ACOUT&LT;&LT;SETW (1) <<a -<<c<<Endl; -COUT&LT;&LT;SETW (9) <<a the<<c<<Endl; -      -      -     return 0; +}

(3) Set the fill character (Setfill)

C + + uses a padding character in a non-data area when the width of the print is greater than the data that is to be placed. The default padding character is a space.

If you want to fill in the characters you want to fill, use this statement.

Cases:

    

1#include <iostream>2#include <iomanip>3 using namespacestd;4 5 intMain ()6 {7     8     intA=123.456;9COUT&LT;&LT;SETW ( -) <<aTen<<"fill is set to a space"<<Endl; OneCOUT&LT;&LT;SETW ( -) <<setfill ('*') <<a A<<"fill is set to ' * '"<<Endl; -      -     return 0; the}

2. Integer operator (dec,oct,hex)

Dec: The decimal operator is the default, which tells the system to print values in decimal.

Oct: The value is printed with an octal numeric system.

Hex: Print with 16 binary.

These operators are set to print until it is reset by another operator (that is, the binary that updates the print).

3. Floating-point operators

(1) Fixed point

The fixed-point operator tells Cout that floating-point numbers are displayed in point numbers instead of floating-point numbers. Floating-point numbers are stored in memory in two parts, that is, logarithms and exponents. Small numbers are displayed in fixed-point format, and large numbers are displayed in floating-point format. When you want to display the number

Very small or very large, the fixed point operator displays two parts that are separated by the exponential sign (e).

1.234568e+06

Most people are not used to this format. The fixed point format for the same number is as follows:

123567.8752

(2) Setting accuracy

The set precision is used to control the small part of the tree to display numbers. As a general rule, C + + uses six decimal digits in his floating-point display. When you use the Set precision operator, C + + uses the same precision for all displays.

(3) Display decimal point

When using 0 precision on floating-point numbers, C + + does not print a decimal point, which makes the floating-point number look like an integer. In order to display the value of the decimal point, we use the display decimal point as shown in:

1#include <iostream>2#include <iomanip>3 using namespacestd;4 5 intMain ()6 {7     floatA=1.0;8     floatb=1.234;9     floatC=1234567.875;Ten      Onecout<<a<<Endl; Acout<<b<<Endl; -cout<<c<<Endl; -cout<<Endl; the          -cout<<fixed; -cout<<a<<Endl; -cout<<b<<Endl; +cout<<c<<Endl; -cout<<Endl; +          ACout<<setprecision (2); atcout<<a<<Endl; -cout<<b<<Endl; -cout<<c<<Endl; -cout<<Endl; -      -cout<<setprecision; incout<<a<<Endl; -cout<<b<<Endl; tocout<<c<<Endl; +cout<<Endl; -      the     return 0; *}

  

Output format for C + +

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.