C++ cout setiosflags() Format Flags

Introduction

Flag Meaning
ios::fixedAlways show the decimal point with six digits after the decimal point. This flag takes precedence if it's set with the ios::showpoint flag.
ios::scientific Use exponential display in the output.
ios::showpointAlways display a decimal point and six significant digits total.
ios::showpos Display a leading + sign when the number is positive.
ios::left Left-justify the output.
ios::rightRight-justify the output.

To combine the flags.

cout << setiosflags(ios::dec | ios::fixed | ios::showpoint); 
#include <iostream>
#include <iomanip>
using namespace std;
int main()/*from w  ww .j av  a2  s.  co  m*/
{
   cout << "|" << 5 <<"|";
   cout << "|" << setw(4) << 5 << "|";
   cout << "|" << setw(4) << 56829 << "|";
   cout << "|" << setw(5)  << setiosflags(ios::fixed) << setprecision(2)  << 5.26 << "|";
   cout << "|" << setw(5)  << setiosflags(ios::fixed) << setprecision(2)  << 5.267 << "|";
   cout << "|" << setw(5)  << setiosflags(ios::fixed) << setprecision(2)  << 53.264 << "|";
   cout << "|" << setw(5)  << setiosflags(ios::fixed) << setprecision(2)  << 534.264 << "|";
   cout << "|" << setw(5)  << setiosflags(ios::fixed) << setprecision(2)  << 534. << "|";
}



PreviousNext

Related