- 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 currency value in Java
To display a currency in Java, use the following DecimalFormat −
DecimalFormat decFormat = new DecimalFormat("\u00a4#,##0.00");
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(877.80) decFormat.format(8.19) decFormat.format(9897.88)
The above will be displayed as −
$877.80 $8.19 $9,897.88
The following is the complete example −
Example
import java.text.DecimalFormat; public class Demo { public static void main(String[] argv) throws Exception { // for currency DecimalFormat decFormat = new DecimalFormat("\u00a4#,##0.00"); System.out.println(decFormat.format(877.80)); System.out.println(decFormat.format(8.19)); System.out.println(decFormat.format(9897.88)); System.out.println(decFormat.format(9.9099)); System.out.println(decFormat.format(12.788)); System.out.println(decFormat.format(9.678)); } }
Output
$877.80 $8.19 $9,897.88 $9.91 $12.79 $9.68
Advertisements
To Continue Learning Please Login
Login with Google