- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display a percentage in Java
To display a percentage in Java, use the following DecimalFormat.
DecimalFormat decFormat = new DecimalFormat("#%");
Since, we have used the DecimalFormat class, therefore do not forget to import the following package −
import java.text.DecimalFormat;
Now, let us learn how to display percentage −
decFormat.format(0.80) decFormat.format(-0.19) decFormat.format(1.88)
The above will be displayed as −
80% -19% 188%
The following is the complete example −
Example
import java.text.DecimalFormat; public class Demo { public static void main(String[] argv) throws Exception { DecimalFormat decFormat = new DecimalFormat("#%"); System.out.println(decFormat.format(0.80)); System.out.println(decFormat.format(-0.19)); System.out.println(decFormat.format(1.88)); System.out.println(decFormat.format(0.9099)); System.out.println(decFormat.format(12.788)); System.out.println(decFormat.format(-9.678)); } }
Output
80% -19% 188% 91% 1279% -968%
- Related Articles
- How to display percentage above a bar chart in Matplotlib?
- Format percentage with MessageFormat class in Java
- Display a currency value in Java
- Java program to calculate the percentage
- How to display a large component within a smaller display area in Java?
- Display a TreeSet in reverse order in Java
- Display the position of a Substring in Java
- Java Program to display a webpage in JEditorPane
- How to display a JRadioButtonMenuItem in Java?\n
- Display tick marks in a JSlider with Java
- Display weekday names in Java
- Display path separator in Java
- Display HashMap elements in Java
- Display the package name of a class in Java
- How to display the items in a JComboBox in Java

Advertisements