I need to print .5
as 0.5
using the printf
statement in C language.
It can be done easily using the following code.
printf("%03f", myfloatvalue);
But the situation is myfloatvalue
is dynamic. So i am not sure whether it is a 3 character number, it could be .5
or .56
or .567
. In the above cases i need to print as 0.5
, 0.56
, and 0.567
.
Continuing from my comment to Florian's answer, the format of
"%0g"
will print the shortest representation with the leading-zero, e.g.Example Use/Output