Take the 2-minute tour ×

How would I go about using the printf() function to print a floating point number in such a way that I only print the decimal part if it is not 0? Examples:

1.0 -> 1
2.0 -> 2
1.5 -> 1.5
2.25 -> 2.25


share|improve this question
Which format specifier are you using right now? Have you tried %g? –  Greg Hewgill Oct 3 '12 at 22:54
add comment

1 Answer

up vote 2 down vote accepted
share|improve this answer
add comment

Your Answer


