柿子饼同学 发表于 2022-4-26 15:22:32

cout 的 fixed 等怎么消除对后面输出的影响

#include <bits/stdc++.h>
using namespace std;
int main(){
      ios::sync_with_stdio(false);
      double a = 3.1415;
      double b = 3333.1415;
      cout << fixed << setprecision(4) << a << endl;
      cout << setprecision(4) << b << endl;
      return 0;
}
如题 , 只写了一个 fixed , 但是影响了后面的输出 , 应该怎么解决呢

人造人 发表于 2022-4-26 15:33:34

http://www.cplusplus.com/reference/ios/fixed/?kw=fixed

The default notation (none) is a different floatfield value than either fixed or scientific. The default notation can be selected by calling str.unsetf(ios_base::floatfield).

傻眼貓咪 发表于 2022-4-26 15:36:56

std::defaultfloat

傻眼貓咪 发表于 2022-4-26 15:38:29

#include <iostream>   // std::cout, std::fixed, std::scientific

int main () {
double a = 3.1415926534;
double b = 2006.0;
double c = 1.0e-10;

std::cout.precision(5);

std::cout << "原样:\n";
std::cout << a << '\n' << b << '\n' << c << '\n';

std::cout << '\n';

std::cout << "fixed:\n" << std::fixed;
std::cout << a << '\n' << b << '\n' << c << '\n';

std::cout << '\n';

std::cout << "原样:\n" << std::defaultfloat;
std::cout << a << '\n' << b << '\n' << c << '\n';
return 0;
}原样:
3.1416
2006
1e-10

fixed:
3.14159
2006.00000
0.00000

原样:
3.1416
2006
1e-10

柿子饼同学 发表于 2022-4-26 15:48:55

傻眼貓咪 发表于 2022-4-26 15:38


感谢~{:10_254:}
页: [1]
查看完整版本: cout 的 fixed 等怎么消除对后面输出的影响