+ 1

Printf float in C

How do I trim the "0" after the coma while printing a float? Setting a precision doesn't work sufficiently because sometimes I need to display four digits after the coma, sometimes it's just two. My float is being divided so I can't predict the exact precision I need, but I need to display it in the shortest form possible without rounding.

23rd Nov 2018, 12:46 PM
Empress
Empress - avatar
1 Odpowiedź
+ 2
Try "%g" #include <stdio.h> int main() { float num1 = 1.39000; printf("%g", num1); return 0; }
23rd Nov 2018, 12:55 PM
Prokopios Poulimenos
Prokopios Poulimenos - avatar