Escape percent sign in String’s format method in java

Escape percent sign in java

In this post, we will see how to escape Percent sign in String’s format() method in java.

Escape Percent Sign in String’s Format Method in Java

String’s format() method uses percent sign(%) as prefix of format specifier.
For example:
To use number in String’s format() method, we use %d, but what if you actually want to use percent sign in the String.

If you want to escape percent sign in String’s format method, you can use % twice (%%).

Let’s see with the help of example:

Output:

10 out of 100 is 10%

As you can see, we have used %% to escape percent symbol in 10%.

Escape Percent Sign in printf() Method in Java

You can apply same logic in printf method to print percent sign using System.out.printf() method.

Output:

10 out of 100 is 10%

That’s all about How to escape percent sign in String’s format method in java.

Was this post helpful?

Related Posts

  • 24 September

    Display Negative Number in Parentheses in Java

    In this post, we will see how to display negative number in Parentheses in Java. We can use pattern #,##0.00;(#,##0.00) with DecimalFormat‘s format() to display negative number in Parenthesis in Java. [crayon-632f4e86a5983977860308/] Output [crayon-632f4e86a598e932385858/] If you don’t need decimal places, you can change to pattern to: [crayon-632f4e86a5990387547498/] Let’s understand meaning of pattern #,##0.00;(#,##0.00): First part […]

  • 24 September

    Increment for Loop by 2 in Java

    Table of ContentsHow to increment for loop by 2 in JavaHow to increment for loop by n in Java In this post, we will see how to increment for loop by 2 in Java. There are several looping statements available in Java and one of them is for loop in java. There are three parts […]

  • 24 September

    Get List of Month Names in Java

    Table of ContentsUsing DateFormatSymbols’s getMonth() methodUsing DateFormatSymbols’s getShortMonths() method [ For short names]Using DateFormatSymbols’s getMonth() with Locale In this post, we will see how to get list of months name in Java. Using DateFormatSymbols’s getMonth() method We can use DateFormatSymbols's getMonth() to get list of months in Java. It will provide complete month names like […]

  • 21 September

    Convert 0 and 1 to boolean in Java

    Table of ContentsConvert 0 and 1 to Boolean in JavaUtility method to convert all possible values to boolean in Java In this post, we will see how to convert 0 and 1 to Boolean in Java. Given “0” and “1” in String datatype, convert it to boolean datatype where “0” represents false “1” represents true […]

  • 20 September

    Replace Backslash with Forward Slash in Java

    Table of Contentsways to replace backslash with forward slash in java1. Using replace() method2. Using replaceAll() method Learn about how to replace backslash with forward slash in java. backslash() is used as escape character in Java. For example it can be used to: Used in special characters such as new line character \n, tab \t […]

  • 20 September

    Remove Backslash from String in Java

    Table of ContentsWays to Remove Backslash from String in Java1. Using replace() method2. Using replaceAll() method Learn about how to remove backslash from String in Java. backslash() is used as escape character in Java. For example it can be used to: Create special characters such as new line character \n, tab \t Write unicode characters […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.