【问题标题】:cout.setf(ios::fixed) and cout << fixed do not seem to get the same result? [closed]cout.setf(ios::fixed) 和 cout << fixed 似乎没有得到相同的结果? [关闭]
【发布时间】:2015-09-20 18:32:15
【问题描述】:

这是我的主要示例:

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" );

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

【问题讨论】:

  • 你的minimal testcase在哪里?
  • 这是我的最小测试用例:双小时 = 35.45; cout.setf(ios::scientific); cout.setf(ios::fixed); cout
  • ....... 阅读我刚刚链接到您的页面。完全地。这不是最小的测试用例。

标签: c++ formatting cout outputstream


【解决方案1】:

标志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时,它相当于这个调用。

【讨论】:

    猜你喜欢
    • 2012-04-28
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    相关资源
    最近更新 更多