Printf float in C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 Answer
+ 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