Formatting Floating-Point Output, show 3 digits to the right, Scientific mode - C++ File Stream

C++ examples for File Stream:cout

Description

Formatting Floating-Point Output, show 3 digits to the right, Scientific mode

Demo Code

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main() {/* w  w w  .j  a v  a 2s .  co  m*/

   ios_base::fmtflags flags =  cout.flags();

   double pi = 3.14159265;

   cout << "pi = " << setprecision(5)  // Normal (default) mode; only
        << pi << '\n';                 // show 5 digits, including both sides of decimal point.

   cout << "pi = " << fixed            // Fixed-point mode;
        << showpos                     // show a "+" for positive nums,
        << setprecision(3)             // show 3 digits to the *right*
        << pi << '\n';                 // of the decimal.

   cout << "pi = " << scientific       // Scientific mode;
        << noshowpos                   // don't show plus sign anymore
        << pi * 1000 << '\n';

   cout.flags(flags);  // Set the flags to the way they were
}

Result


Related Tutorials