Decimal places

In Lab 4 you will be displaying double values. These can have rather long decimal values. Java provides a way to format these numbers so that they are more managable for printing. Here is an example:

// you must use this import line at the top of the ComputeFigure class

import java.text.*;


// For setting 2 decimal places for the format.
//you can put this at the beginning of the paint() method.

NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);

//use this (replace area with whatever variable you are using)
//to print out the formatted information.

g.drawString("Area = " + nf.format(area), 120,120);

See http://java.sun.com/j2se/1.4/docs/api/java/text/NumberFormat.html for SDK v1.3