Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
You obviously can’t put just anything after the “%” sign. The syntax for what goes after the percent sign follows very specific rules, and describes how to format the argument that gets inserted at that point in the result (formatted) String.
You’ve already seen two examples:
%, d means “insert commas and format the number as a decimal integer.”
and
%.2f means “format the number as a floating point with a precision of two decimal places.”
and
%,.2f means “insert commas and format the number as a floating point with a precision of two decimal places.”
The real question is really, “How do I know what to put after the percent sign to get it to do what I want?” And that includes knowing the symbols (like “d” for decimal and “f” for floating point) as well as the order in which the instructions must be placed following the percent sign. For example, if you put the comma after the “d” like this: “%d,” instead of “%,d” it won’t work!