Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Literal percent sign in printf statement

People also ask

How do I print a percent sign in printf?

To obtain the character "%" in a format string, enter "%%", e.g. printf("The percentage is %d %%. *n",5); yields The percentage is 5 %.

How do you express a percent sign (%) in a format specifier?

The % symbol has a special use in a printf statement. How would you place this character as part of the output on the screen? You can do this by using %% in the printf statement. For example, you can write printf(“10%%”) to have the output appear as 10% on the screen.

How do you show the percentage sign in Java?

The percent sign is escaped using a percent sign: System. out. printf("%s\t%s\t%1.2f%%\t%1.2f%%\n",ID,pattern,support,confidence);


The percent sign is escaped using a percent sign:

System.out.printf("%s\t%s\t%1.2f%%\t%1.2f%%\n",ID,pattern,support,confidence);

The complete syntax can be accessed in java docs. This particular information is in the section Conversions of the first link.

The reason the compiler is generating an error is that only a limited amount of characters may follow a backslash. % is not a valid character.


Escaped percent sign is double percent (%%):

System.out.printf("2 out of 10 is %d%%", 20);

You can use StringEscapeUtils from Apache Commons Logging utility or escape manually using code for each character.