import java.text.DecimalFormat; import java.text.NumberFormat; public class NumberFormat2 { public static void main(String[] args) { // In 'DecimalFormat' patterns, before the decimal point, the "#" // character will display any number, but leading zeros will not be // displayed; the "0" character will display the remaining digits, // and they will each display as zero if no digit is available; // after the decimal point, the "#" character will not display // trailing zeros, and will round, if necessary; the "0" character // will display a zero for each digit that is not available, and // will round, if necessary; when used before the decimal point, "#" // represents any number of digits, while "0" represents a single // digit; however, a longer number will not be cut short, even if // too few zeros exist before the decimal point NumberFormat currency = new DecimalFormat("$#,##0.00"); System.out.println(currency.format(387)); System.out.println(currency.format(12.5)); System.out.println(currency.format(0.91)); System.out.println(currency.format(604.50)); System.out.println(currency.format(5432.1)); System.out.println(currency.format(2345678.567)); } }