ios_base::precision
Specifies the number of digits to display in a floating-point number.
streamsize precision( ) const;
streamsize precision(
streamsize _Prec
);
Parameters
_Prec
The number of significant digits to display, or the number of digits after the decimal point in fixed notation.
Return Value
The first member function returns the stored display precision. The second member function stores _Prec in the display precision and returns its previous stored value.
Example
Copy Code
// ios_base_precision.cpp
// compile with: /EHsc
#include <iostream>
int main( )
{
using namespace std;
float i = 31.31234F;
cout.precision( 3 );
cout << i << endl; // display three significant digits
cout << fixed << i << endl; // display three digits after decimal
// point
}
Output
31.3
31.312
///////////////////////////////////////////////////////////////////////////////
ios_base::setf
Sets the specified flags.
void setf(
fmtflags _Mask
);
fmtflags setf(
fmtflags _Mask,
fmtflags _Unset
);
Parameters
_Mask
The flags to turn on.
_Unset
The flags to turn off.
Return Value
The previous format flags
Example
Copy Code
// ios_base_setf.cpp
// compile with: /EHsc
#include <iostream>
int main( )
{
using namespace std;
int i = 10;
cout << i << endl;
cout.unsetf( ios_base::dec );
cout.setf( ios_base::hex );
cout << i << endl;
cout.setf( ios_base::dec );
cout << i << endl;
cout.setf( ios_base::hex, ios_base::dec );
cout << i << endl;
}
Output
10
a
10
a
/////////////////////////////////-----msdn----//////////////////////////////////////////////