function
std::fixed
<ios>
ios_base& fixed ( ios_base& str );
Use fixed-point notation
Sets the
floatfield format flag for the
str stream to
fixed.
When
floatfield is set to
fixed, float values are written using fixed-point notation, which means the value is represented with exactly as many digits in the fraction part as specified by the
precision field and with no exponent part.
The
floatfield format flag is both a selective and a toggle flag, so it can take any of its two possible following values (using the manipulators
fixed and
scientific), or none of them (using
ios_base::unsetf):
flag value | effect when set |
fixed | write floating point values in fixed-point notation. |
scientific | write floating-point values in scientific notation. |
(none) | write floating-point values in default floating-point notation. |
The
floatfield flag is
not set in standard streams on initialization.
The
precision field can be modified using the
ios_base::precision member of the stream.
Notice that the treatment of the
precision field differs between the default floating-point notation and the fixed and scientific notations. On the default floating-point notation, the
precision field specifies the maximum number of meaningful digits to display both before and after the decimal point, while in both the fixed and scientific notations, the
precision field specifies exactly how many digits to display
after the decimal point, even if they are trailing decimal zeros.
Parameters
- str
- Stream object where to apply.
Because this function is a manipulator, it is designed to be used alone with no arguments in conjunction with the insertion (<<) and extraction (>>) operations on streams (see example below).
Return Value
A reference to the stream object.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
// modify basefield
#include <iostream>
using namespace std;
int main () {
double a,b,c;
a = 3.1415926534;
b = 2006.0;
c = 1.0e-10;
cout.precision(5);
cout << a << '\t' << b << '\t' << c << endl;
cout << fixed << a << '\t' << b << '\t' << c << endl;
cout << scientific << a << '\t' << b << '\t' << c << endl;
return 0;
}
|
The execution of this example displays something similar to:
3.1416 2006 1e-010
3.14159 2006.00000 0.00000
3.14159e+000 2.00600e+003 1.00000e-010 |
See also
- scientific
- Use scientific notation (function)
- ios_base::flags
- Get/set format flags (public member function)
- ios_base::setf
- Set specific format flags (public member function)
- ios_base::unsetf
- Clear specific format flags (public member function)