Display a currency value in Java

Java 8Object Oriented ProgrammingProgramming

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

 Live Demo

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
raja
Published on 21-Dec-2018 11:04:22
Advertisements