how to print 09 if the int variable is 9 in C?
any existing fu开发者_运维百科nction to do that in c?
sprintf with formatting codes like %02d will give you two decimal places.
here's an example of the command sprintf( string, "file.%d", file_number );
Here it puts the string "file.2" into the variable named string, assuming that 2 was in the variable named file_number.
you can use multiple like so: sprintf(str, "%02d/%02d/%4d",day,month,year);
Look up the specs on sprintf for other kinds of formatting like floating point significant digits.
Along with "%02d"
you can use "%2.2d"
if you prefer. The latter style is handy when/if the actual width is in a variable so you do something like this:
int width = 2;
int value = 9;
printf("%*.*d", width, width, value);
printf("%02d", 9);
精彩评论