Format number as a currency in Java

Description

The following code shows how to format number as a currency.

Example


//  ww  w. j a v a  2  s  .co m
import java.text.NumberFormat;

public class Main {
  public static void main(String[] argv) throws Exception {

    NumberFormat nf = NumberFormat.getCurrencyInstance();

    System.out.println("currency format: " + nf.format(1234.56) + " "
        + nf.format(-1234.56));
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Data Format »




Java Formatter
Java Number Formatter