Currency Formats

If you know you're going to be working with money, you can request a currency formatter with the static NumberFormat.getCurrencyInstance() method:

public static final NumberFormat getCurrencyInstance()
public static NumberFormat getCurrencyInstance(Locale inLocale)
import java.text.*;
import java.util.*;

public class MinimumWage {

  public static void main(String[] args) {
  
    NumberFormat dollarFormat = NumberFormat.getCurrencyInstance(Locale.ENGLISH);
    double minimumWage = 5.15;
    
    System.out.println("The minimum wage is " 
     + dollarFormat.format(minimumWage));
    System.out.println("A worker earning minimum wage and working for forty");
    System.out.println("hours a week, 52 weeks a year, would earn " 
     + dollarFormat.format(40*52*minimumWage));
    
  }

}

Output:
This program prints
The minimum wage is $5.15
A worker earning minimum wage and working for forty 
hours a week, 52 weeks a year, would earn $10,712.00

Previous | Next | Top | Cafe con Leche

Copyright 2000 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified January 29, 2000