ztik ztik - 7 months ago 62
C++ Question

Control exponent digits of a float using printf in gcc

I am using

mingw-gcc
and I want to print a
float
.

#include <cstdio>
#include <iostream>

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


the output is

1.230000e+000
1.230000e+000


However float does not need more than two digits.
Is there any way in
gcc
to export number with 2 digit exponent?

1.230000e+00
1.230000e+00


Is there any similar function like _set_output_format of Visual studio?

Answer

at least as far as printf is concerned, it seems that one can set the environment variable PRINTF_EXPONENT_DIGITS to 2 and compile with -posix switch (tested with mingw g++ 5.3.0 on clean install of Windows 10)