C++で小数点以下の桁数を指定して出力
C++で小数点以下の桁数を指定して出力
有効ビット別出力はsetprecisionであり、小数点以下のビット数で出力もsetprecisionであるが、誰がfixedに依存するのか.cout
テストコード:
有効ビット別出力はsetprecisionであり、小数点以下のビット数で出力もsetprecisionであるが、誰がfixedに依存するのか.cout
テストコード:
#include
#include
using namespace std;
int main( void )
{
const double value = 12.3456789;
cout << value << endl; // 6 , 12.3457
cout << setprecision(4) << value << endl; // 4 , 12.35
cout << setprecision(8) << value << endl; // 8 , 12.345679
cout << fixed << setprecision(4) << value << endl; // fixed , , 12.3457
cout << value << endl; // fixed setprecision , 12.3457
cout.unsetf( ios::fixed ); // fixed, , 12.35
cout << value << endl;
cout.precision( 6 ); // , 12.3457
cout << value << endl;
}