What single format string can be used to print 0.0, 1.0, 0.025 ? eg given:
float vals[3] = { 0.0f, 1f, 0.025f };
for(int i=0; i < 3; i++)
printf("%g\n", vals[i]);
desired output:
0.0
1.0
0.025
but instead get:
0
1
0.025
Have tried various width specifiers and %f vs %g but can't get what I want.
If there's a c++ alternative that's fine to.
You can use * to tell printf the precision will come from an int argument value.
printf("%.*f\n", (vals[i] == (int)vals[i]) ? 1 : 3 ,vals[i]);
should print:
0.0
1.0
0.025