The NumberFormat class can be used to control currency output.

 

Example:

 

 

import java.text.*;  

public class FormattedOutput{

  public static void main(String[] args) {

  int qty;

  double price, amountDue;

  double x = 10000.0/3.0;

  System.out.println("Default output is \t" + x);

  // prints 3333.3333333333335

 

  NumberFormat num_format = NumberFormat.getNumberInstance(); 

  String s = num_format.format(x);

  System.out.println("Number format is \t" + s);

  // prints 3,333.333

 

  NumberFormat cur_format = NumberFormat.getCurrencyInstance();

  s = cur_format.format(x);

  System.out.println("Currency format is \t" + s);

  // prints $3,333.33

 

  NumberFormat per_format = NumberFormat.getPercentInstance();

  s = per_format.format(x);

  System.out.println("Percent format is \t" + s); 

  // prints 333,333%

  }

}