在gcc中使用printf控制浮点数的指数位数

Control exponent digits of a float using printf in gcc

本文关键字:浮点数 指数 控制 printf gcc      更新时间:2023-10-16

我使用mingw-gcc,我想打印一个float

#include <cstdio>
#include <iostream>
int main(){
  float a =1.23;
  std::cout << std::scientific << a << std::endl;
  printf("%en",a);
  return 0;
}

输出为

1.230000e+000
1.230000e+000

但是float不需要超过两位数字。是否有任何方法在gcc出口数字与2位指数?

1.230000e+00
1.230000e+00

是否有类似的功能像Visual studio的_set_output_format ?

至少就printf而言,似乎可以将环境变量PRINTF_EXPONENT_DIGITS设置为2并使用-posix开关进行编译(在干净安装的Windows 10上使用mingwg++ 5.3.0进行测试)