Output streams save data from a program to a file, formatting it as they save it. The default format for floating point numbers, float
, double
, and long double
, is decimal (base-10), six significant digits (aka significant figures or sig figs), and scientific notation for very large or very small (near zero) numbers. Stream objects maintain a set of 1-bit formatting flags. Each flag is a binary switch - on or off - controlling some output format. Programmers can change an output stream's floating point format by setting the switches. Although cout is permanently attached to the console, it is an otherwise typical output stream, suggesting that programmers may use the features described here with all output streams. The following example demonstrates some of the formatting options.
Feature | Comment |
---|---|
ios::fixed | A bitmask corresponding to the fixed flag. Setting the flag prevents displaying numbers in scientific notation regardless of their size. |
ios::scientific | A bitmask corresponding to the scientific flag. Setting the flag forces displaying numbers in scientific notation independent of their size. |
Setting both fixed and scientific flags "on" causes hexadecimal (base-16) output. | |
setf(f) | The function, short for set flags, sets flags to 1 or "on." |
unsetf(f) | The function, short for unset flags, sets a flag to 0 or "off." |
flags() | Returns an integer whose bits correspond to the current formatting flags. |
precision(n) | Sets the number of significant digits in the output. The precision setting does not affect how integers are displayed. |
Compare to the fixed and scientific manipulators: notation.cpp.
Program | Output | Comments |
---|---|---|
#define _USE_MATH_DEFINES #include <iostream> #include <cmath> using namespace std; int main() { cout << "Default settings" << endl; if (cout.flags() & ios::fixed) cout << "fixed flag set" << endl; else cout << "fixed flag unset" << endl; if (cout.flags() & ios::scientific) cout << "scientific flag set" << endl; else cout << "scientific flag unset" << endl; cout << 123456789 << endl; cout << 123456789. << endl; cout << M_PI << endl; cout << endl; cout << 1.0/3.0 << endl; cout << 10.0/3.0 << endl; cout << 100.0/3.0 << endl; cout << 1.0/300000.0 << endl; cout << endl; |
Default settings fixed flag unset scientific flag unset 123456789 1.23457e+08 3.14159 0.333333 3.33333 33.3333 3.33333e-06 |
True if fixed is set True if scientific is set Integer output Automatic scientific notation Six significant digits: 3, 1, 4, 1, 5, 9 Six significant digits: "333333" Ditto Ditto Automatic scientific notation |
cout.precision(2); if (cout.flags() & ios::fixed) cout << "fixed flag set" << endl; else cout << "fixed flag unset" << endl; if (cout.flags() & ios::scientific) cout << "scientific flag set" << endl; else cout << "scientific flag unset" << endl; cout << 123456789 << endl; cout << 123456789. << endl; cout << M_PI << endl; cout << endl; |
fixed flag unset scientific flag unset 123456789 1.2e+08 3.1 |
Two significant digits in the output Integer format unchanged Two significant digits: 1 and 2 Two significant digits: 3 and 1 |
cout.setf(ios::fixed); if (cout.flags() & ios::fixed) cout << "fixed flag set" << endl; else cout << "fixed flag unset" << endl; if (cout.flags() & ios::scientific) cout << "scientific flag set" << endl; else cout << "scientific flag unset" << endl; cout << 123456789 << endl; cout << 123456789. << endl; cout << M_PI << endl; cout << endl; |
fixed flag set scientific flag unset 123456789 123456789.00 3.14 |
Fixed point output (not scientific) Large numbers not printed in scientific notation |
cout << "Hexadecimal output" << endl; cout.setf(ios::scientific); if (cout.flags() & ios::fixed) cout << "fixed flag set" << endl; else cout << "fixed flag unset" << endl; if (cout.flags() & ios::scientific) cout << "scientific flag set" << endl; else cout << "scientific flag unset" << endl; cout << 123456789 << endl; cout << 123456789. << endl; cout << M_PI << endl; cout << endl; |
Hexadecimal output fixed flag set scientific flag set 123456789 0x1.d7p+26 0x1.92p+1 |
Both flags set, "fixed" set above Hexadecimal (base-16) output Ditto |
cout << "Decimal output" << endl; cout.unsetf(ios::fixed); cout.setf(ios::scientific); if (cout.flags() & ios::fixed) cout << "fixed flag set" << endl; else cout << "fixed flag unset" << endl; if (cout.flags() & ios::scientific) cout << "scientific flag set" << endl; else cout << "scientific flag unset" << endl; cout << 123456789 << endl; cout << 123456789. << endl; cout << M_PI << endl; return 0; } |
Decimal output fixed flag unset scientific flag set 123456789 1.23e+08 3.14e+00 |
Switches off (i.e., sets to 0) the fixed flag Integer format unaffected Scientific notation Small numbers display in scientific notation |