C + + cout formatted output method

Source: Internet
Author: User

C language can use printf (),%f to achieve the format of floating point number output, with cout ...?

The Iomanip is the I/O flow control header file, just like the formatted output of printf.

Here are some common ones:

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
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

1#include <iostream>2#include <iomanip>3 using namespacestd;4 DoubleD=11.23456;5 intMain ()6 {7cout<<d<<endl;//Direct output can only output 6 digits, including integer and fractional parts8Cout<<setprecision (3) <<d<<endl;//accuracy of 3, output 3-digit9Cout<<setiosflags (iOS::fixed) <<d<<endl;//Precision 3, fixed-point output, 3-bit fractional outputTenCout<<setiosflags (iOS::fixed) <<setprecision (7) <<d<<endl;//the number of digits is not enough, add 0 at the end One     return 0; A}

C + + cout formatted output method

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.