Alex Rivera | Logout

Java: Literal percent sign in printf statement

Asked 2009-11-10T14:41:16.450
169

I'm trying to add an actual percent sign into a printf statement in Java and I'm getting the error:

lab1.java:166: illegal escape character
                System.out.printf("%s\t%s\t%1.2f\%\t%1.2f\%\n",ID,pattern,support,confidence);
                                                 ^
lab1.java:166: illegal escape character
                System.out.printf("%s\t%s\t%1.2f\%\t%1.2f\%\n",ID,pattern,support,confidence);
                                                          ^
2 errors

I can't figure out how to put an actual percent sign into my printf? I thought using \% to escape it would work, but it isn't.

Any ideas?

Edit
Report

1 Answer

131

Escaped percent sign is double percent (%%):

System.out.printf("2 out of 10 is %d%%", 20);
answered 2009-11-10T14:42:52.570

Your Answer