Some methods of cout formatting

Source: Internet
Author: User
Tags setf

There are many ways to format cout, compared to printf in C, which is easier to understand in the way it is implemented.

1, counting into the system.

1, 16 binary: Hex

2, octal: Oct

3, decimal: Dec (default)

There are two types of formats that can be used when controlling the binary:

(1) Hex (cout); It can also be an Oct (cout) or Dec (cout)

(2) cout<

Example code:

  

1#include <cstdio>2#include <iostream>3#include <ctime>4#include <fstream>5#include <ostream>6 using namespacestd;7 8 9 intMain () {Ten     Const intN = -; One     //The first method of implementation A Hex (cout); -cout<<"Hex:"<<N<<Endl; - Oct (cout); thecout<<"Oct:"<<N<<Endl; - Dec (cout); -cout<<"Dec:"<<N<<Endl; -     //The second way of realization +cout<"Hex:"<<N<<Endl; -cout<<oct<<"Oct:"<<N<<Endl; +cout<<dec<<"Dec:"<<N<<Endl; A return 0;  at}

2, adjust the sub-width

Cout.width (int width); Only one project at a time

Example code:

1#include <cstdio>2#include <iostream>3#include <ctime>4#include <fstream>5#include <ostream>6 using namespacestd;7 8 9 intMain () {Ten     //cout<<width (5) <<12<<endl; OneCout.width ( A); Acout<<121212121<<Endl; -     return 0;  -}

3. Fill characters:

Cout.fill (char t);

Example code:

 #include <cstdio> #include  <iostream>  #include  <ctime> #include  < Fstream> #include  <ostream>using  namespace   Std;  int   Main () {Cout.fill ( "  *   " );    Cout.width ( 12  );    cout  <<12  <<ENDL;  return  0  ;}  

4, set the precision of floating point number:

cout.precision (int bit);

Example code:

#include <iostream>usingnamespace  std; int Main () {    cout.precision (2);    cout<<1.233<<Endl;     return 0 ;}

5, print the end of the 0 and the decimal point

COUT.SETF (Ios_base::showpoint);

Example code:

 1  #include <iostream>2  using  namespace   Std;  3   Main () { 4   COUT.SETF (ios_base:: Showpoint);  5  cout<<endl;  6  cout<<endl;  7  return  0   8 } 

6. Parameters of the SETF () function (single parameter version)

(1) Ios_base::boolalpha displays a string value of type bool (TRUE/FALSE)

(2) ios_base::showbase display numeric cardinality prefix (octal prefix 0, hex prefix 0x)

(3) Ios_base::showpoint shows the decimal point and the end of the 0

(4) Ios_base::uppercase for hexadecimal number display capital letters

(5) Ios_base::showpos Display the preceding + sign for positive numbers (base 10 o'clock)

Some methods of cout formatting

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.