You can use the %g
format specifier:
#include <stdio.h>
int main() {
float f1 = 12;
float f2 = 12.1;
float f3 = 12.12;
float f4 = 12.1234;
printf("%g
", f1);
printf("%g
", f2);
printf("%g
", f3);
printf("%g
", f4);
return 0;
}
Result:
12
12.1
12.12
12.1234
Note that, unlike the f
format specifier, if you specify a number before the g
it refers to the length of the entire number (not the number of decimal places as with f
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…