【问题标题】:Control exponent digits of a float using printf in gcc在 gcc 中使用 printf 控制浮点数的指数位
【发布时间】:2016-09-16 14:04:00
【问题描述】:

我正在使用mingw-gcc,我想打印float

#include <cstdio>
#include <iostream>

int main(){
  float a =1.23;
  std::cout << std::scientific << a << std::endl;
  printf("%e\n",a);
  return 0;
}

输出是

1.230000e+000
1.230000e+000

但是浮点数不需要超过两位数。 gcc有没有办法导出2位指数的数字?

1.230000e+00
1.230000e+00

有没有类似Visual Studio的_set_output_format的功能?

【问题讨论】:

标签: c++ printf


【解决方案1】:

至少就printf 而言,seems 可以将环境变量PRINTF_EXPONENT_DIGITS 设置为2 并使用-posix 开关进行编译(在全新安装时使用mingw g++ 5.3.0 进行了测试) Windows 10)

【讨论】:

  • 这取决于库,与编译器无关。这绝对不是标准的。
  • 谢谢。我试过了,它适用于setenvputenv。它也适用于cout
猜你喜欢
  • 1970-01-01
  • 2013-05-01
  • 2015-06-09
  • 2012-04-15
  • 2014-01-02
  • 1970-01-01
  • 2013-08-26
  • 2015-09-28
相关资源
最近更新 更多