Usage of setiosflags () in C + +

Source: Internet
Author: User
Tags setf


Cout<<setiosflags (ios::fixed) <<setiosflags (ios::right) <<setprecision (2);

Setiosflags is a C + + operator that is contained in the namespace Iomanip, which is the function of executing a parameter specified by the

Action within the area;

Iso::fixed is one of the parameters of the operator setiosflags, which specifies that the action is to represent a floating point in the form of a decimal point

To the right of the decimal point as far as possible within the allowable accuracy range;

Iso::right is also a setiosflags parameter that specifies the right-aligned output within the specified area.

Setprecision is also a C + + operator contained in the namespace Iomanip, which functions as a set of floating-point numbers;

Setprecision (2) means the precision of the decimal point output, that is, the number of digits to the right of the decimal point is 2.

Cout<<setiosflags (ios::fixed) <<setiosflags (ios::right) <<setprecision (2);

Together, the output is a floating-point number with a right-aligned two-digit decimal point.

Use Setprecision (n) to control the number of floating-point numbers displayed by the output stream. The default stream output value for C + + is 6.

If Setprecision (n) is combined with setiosflags (ios::fixed), you can control the number of digits to the right of the decimal point.

Setiosflags (ios::fixed) is a fixed-point representation of real numbers

Add:

Q: What is the role of COUT.SETF () and Cout.precision () in C + +?

For:

These two are the ~ostream member functions of the format control, you can also use the output stream operator to control, all the same ~ attached to you some look ~

Which COUT.SETF and setiosflags the same, cout.precision and setprecision like ~

#include <iomanip>

The Iomanip is more useful in this:

Mainly for cin,cout, such as some manipulation operators, such as setfill,setw,setbase,setprecision and so on. This is the I/O flow control header

Like the formatted output in C. Here are some common control functions:

Dec base is 10 equivalent to "%d"

Hex base is 16 equivalent to "%x"

The OCT has a base of 8 equivalent to "%o"

Setfill (c) Set the fill character to C

Setprecision (n) Set display decimal precision is n bit

SETW (n) Set a field width of n characters

This control means that the output width is guaranteed to be N. Such as:

COUT<<SETW (3) <<1<<SETW (3) <<10<<SETW (3) <<100; The output result is

1 10100 (the default is right-aligned) when the output length is greater than 3 o'clock (<<1000), SETW (3) does not work.

Setioflags (ios::fixed) fixed floating-point display

Setioflags (ios::scientific) index indicates

Setiosflags (ios::left) Align Left

Setiosflags (ios::right) Align Right

Setiosflags (IOS::SKIPWS ignores leading whitespace

Setiosflags (ios::uppercase) 16 Decimal Capital Output

Setiosflags (ios::lowercase) 16 binary lowercase output

Setiosflags (ios::showpoint) force display decimal point

Setiosflags (ios::showpos) force display symbols

Example:

#include <iostream>

#include <iomanip>

using namespace Std;

int main ()

{

cout<<12345.0<<endl;//Output "12345"

Cout<<setiosflags (ios::fixed) <<setprecision (3) <<1.2345<<endl; output "1.235"

Cout<<setiosflags (ios::scientific) <<12345.0<<endl;//output "1.234500e+

004 "

Cout<<setprecision (3) <<12345.0<<endl;//output "1.235e+004"

return 0;

}

Usage of setiosflags () in C + +

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.