The f
specification is for floating-point numbers.
Note that x
s are included in the printf
statement to clarify
the placement of padding characters.
Display float or double:
printf ("x %f x\n", 3.14159); printf ("x %f x\n", -3.14159); --- Result --- x 3.141590 x x -3.141590 x
Display with two digits following the decimal point:
printf ("x %.2f x\n", 3.14159); printf ("x %.2f x\n", -3.14159); --- Result --- x 3.14 x x -3.14 x
Display with spaces, if necessary, to fill a six-character field:
printf ("x %6.2f x\n", 3.14159); printf ("x %6.2f x\n", -3.14159); --- Result --- x 3.14 x x -3.14 x
Display with spaces, if necessary, to fill a six-character field; if more than six characters are involved, display them all anyway:
printf ("x %6f x\n", 3.14159); printf ("x %6f x\n", -3.14159); --- Result --- x 3.141590 x x -3.141590 x
Display with spaces on the right, if necessary:
printf ("x %-6.2f x\n", 3.14159); printf ("x %-6.2f x\n", -3.14159); --- Result --- x 3.14 x x -3.14 x
Always include sign:
printf ("x %+6.2f x\n", 3.14159); printf ("x %+6.2f x\n", -3.14159); --- Result --- x +3.14 x x -3.14 x
Always include sign and pad on the right:
printf ("x %+-6.2f x\n", 3.14159); printf ("x %+-6.2f x\n", -3.14159); --- Result --- x +3.14 x x -3.14 x