cout.setf(ios::fixed) 和 cout << fixed 似乎没有得到相同的结果?

cout.setf(ios::fixed) and cout << fixed do not seem to get the same result?

这是我的主要样本:

double hours = 35.45;
double rate = 15.00;
double tolerance = 0.01000;

cout.setf(ios::scientific);
cout << "Scientific notation: " << endl;
cout << "hours = " << hours << ", rate = " << rate << ", pay = "
    << hours * rate << ", tolerance = " << tolerance << endl << endl;

cout.setf( ios::fixed ); // if i replace but cout << fixed, it works
cout << setprecision( 3 );
cout << "Fixed decimal notation: " << endl;
cout << "hours = " << hours << ", rate = " << rate << ", pay = "
    << hours * rate << ", tolerance = " << tolerance << endl << endl;

system( "pause" );

为什么不相等?如果它们相等那么这里有什么问题?

标志 std::ios_base::fixed 只是 std::ios_base::floatfield 中的两个标志之一。函数 std::ios_base::setf() 忽略了不同标志之间的关系,只是设置了一个位模式。如果您想设置 std::ios_base::fixed 并清除子组中的其他字段,您可以使用

std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);

当你使用操纵器时std::fixed相当于这个调用