Java string format percent symbol

How do I escape / display percent sign in printf statement?

You have a problem displaying the % sign when you want to print a number in percentage format using the printf() method. Because the % sign is use as a prefix of format specifiers, you need to escape it if you want to display the % sign as part of the output string.

To escape the percent sign ( % ) you need to write it twice, like %% . It will print out a single % sign as part of your printf() method output. Let see an example in the code snippet below:

package org.kodejava.lang; public class EscapePercentSignExample < public static void main(String[] args) < String format = "The current bank interest rate is %6.2f%%.%n"; System.out.printf(format, 10f); >> 

In the code snippet above we use the following format %6.2f%%.%n which can be explained as:

  • %6.2f format the number ( 10f ) as six characters in width, right justified, with two places after decimal point. The f conversion character means it accept a float value.
  • %% will escape the % sign and print it as part of the output.
  • %n will print out a new line character.
Читайте также:  Php utf 8 пробелы

When you execute the code, it will print:

The current bank interest rate is 10.00%. 

A programmer, recreational runner and diver, live in the island of Bali, Indonesia. Programming in Java, Spring, Hibernate / JPA. You can support me working on this project, buy me a cup of coffee ☕ every little bit helps, thank you 🙏

Источник

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:

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

Further reading:

How to escape double quotes in String in java
Print double quotes in java

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.

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

Was this post helpful?

Share this

Author

Count Files in Directory in Java

Count Files in Directory in Java

Table of ContentsUsing java.io.File ClassUse File.listFiles() MethodCount Files in the Current Directory (Excluding Sub-directories)Count Files in the Current Directory (Including Sub-directories)Count Files & Folders in Current Directory (Excluding Sub-directories)Count Files & Folders in Current Directory (Including Sub-directories)Use File.list() MethodUsing java.nio.file.DirectoryStream ClassCount Files in the Current Directory (Excluding Sub-directories)Count Files in the Current Directory (Including Sub-directories)Count […]

Читайте также:  Php удалить переменную массив

Convert System.nanoTime to Seconds in Java

Table of ContentsIntroductionSystem.nanoTime()Dividing the System.nanoTime() with a Constant ValueUsing the convert() Method of Time Unit Class in JavaUsing the toSeconds() Method of Time Unit Class in JavaUsing the Utility Methods of Duration Class in Java Introduction In this article, we will look into How to Convert System.nanoTime() to Seconds in Java. We will look at […]

Update Value of Key in HashMap in Java

Table of ContentsUsing the put() Method of HashMap Collection in JavaUsing the compute() Method of HashMap Collection in JavaUsing the merge() Method of the HashMap Collection in JavaUsing the computeIfPresent() Method of The HashMap Collection in JavaUsing the replace() Method of The HashMap Collection in JavaUsing the TObjectIntHashMap Class of Gnu.Trove Package in JavaUsing the […]

How to Get Variable From Another Class in Java

Table of ContentsClasses and Objects in JavaAccess Modifiers in JavaGet Variable From Another Class in JavaUsing the Default or Public Access Modifier of the Other ClassUsing the Static Member of Another ClassUsing the Inheritance Concept of JavaUsing the Getters and Setters of Another ClassUsing the Singleton Pattern Design for Declaring Global VariablesConclusion In this article, […]

Create Array of Linked Lists in Java

Table of ContentsIntroductionLinked List in JavaApplication of Array of Linked ListsCreate Array of Linked Lists in JavaUsing Object[] array of Linked Lists in JavaUsing the Linked List array in JavaUsing the ArrayList of Linked Lists in JavaUsing the Apache Commons Collections Package Introduction In this article, we will look at how to Create an Array […]

Читайте также:  Java string format добавить пробелы

Check if Date Is Between Two Dates in Java

Table of ContentsIntroductionDate & Local Date Class in JavaCheck if The Date Is Between Two Dates in JavaUsing the isAfter() and isBefore() Methods of the Local Date Class in JavaUsing the compareTo() Method of the Local Date Class in JavaUsing the after() and before() Methods of the Date Class in JavaUsing the compare To() Method […]

Источник

Оцените статью