// DecimalFormatting.java // Demos formatting with $ and decimal places // APPLET CODE="DecimalFormatting" HEIGHT=300 WIDTH=400 import java.awt.*; import javax.swing.*; import java.text.DecimalFormat; public class DecimalFormatting extends JApplet { double amount; Container c; JTextField zeroDec, oneDec, twoDec; public void init( ) { c = getContentPane( ); c.setLayout( new FlowLayout( ) ); amount = 77773999.33999999999; //amount = 4.4588; DecimalFormat currency0 = new DecimalFormat( "$0" ); DecimalFormat comma1 = new DecimalFormat( "#,##0.0" ); DecimalFormat currency2 = new DecimalFormat( "$#,##0.00" ); zeroDec = new JTextField( 30 ); oneDec = new JTextField( 30 ); twoDec = new JTextField( 30 ); c.add( zeroDec ); c.add( oneDec ); c.add( twoDec ); zeroDec.setText( "The amount is " + currency0.format( amount ) ); oneDec.setText( "The amount is " + comma1.format( amount ) ); twoDec.setText( "The amount is " + currency2.format( amount ) ); } }