Formatting Decimal Outputs

 

 

Formatting Decimal Output.  In the last webpage we developed two methods to compute the circumference and area of a circle based on user input.  You should have noticed that using double precision results in the display of a lot of not particularly useful digits.  This  webpage will present a bit about the DecimalFormat classes in the java.text package in order to improve on such things.

The program we will work on is just a slight modification of the CircleInfo.java program of the last webpage.   The only changes to the code are highlighted in a purplish color. This first set of code should be called CircleInfoDF.java.

 

import java.awt.Container;
import javax.swing.*;
import java.text.DecimalFormat;

public class CircleInfoDF extends JApplet
{

JTextArea outputArea = new JTextArea( );
double PI = 3.14159;

public void init()
{

String output = "";
String strRadius;
double radius = 0.0;
boolean goodInput = true;



// get the applet's GUI component display area
Container c = getContentPane( );

// attach output area to container c
c.add( outputArea );

double area;
double circumference;

strRadius = JOptionPane.showInputDialog("Please enter the radius of the circle");

try
{

radius = Double.parseDouble(strRadius);

}

catch (NumberFormatException nfeDouble)
{

JOptionPane.showMessageDialog(null, "You need to enter a\nnumber for the radius", "Input Error", JOptionPane.ERROR_MESSAGE);
goodInput = false;

}

if (goodInput)
{

area = circleArea( radius );
circumference = circleCircumference( radius );

// developing a format to set the number of digits to be displayed
DecimalFormat threeDigits = new DecimalFormat("0.000");

output = output + "The radius of the circle is " + radius + "\n\n";
output = output + "The area of the circle is " + threeDigits.format(area) + "\n\n";
output = output + "The circumference of the circle is " + threeDigits.format(circumference);

outputArea.setText(output);

}

}

// method to compute the area
// based on the radius

public double circleArea( double r )
{

return PI * r * r;

}

// method to compute the circumference
// based on the radius

public double circleCircumference( double r )
{

return 2.0 * PI * r;

}

}

 

Before we discuss the program you should develop the HTML file called CircleInfoDF.html.

 

<html>
<applet code="CircleInfoDF.class" width=300 height=150>
</applet>
</html>

 

We rerun the program with the same input of a radius of 5.40 and we get the following

 

 

Which differs quite appropriately from what we got without the DecimalFormat.

 

 

Notice the only real changes to the code are
  • importing java.text.DecimalFormat
  • instantiating a usable format called threeDigits

DecimalFormat threeDigits = new DecimalFormat("0.000");

  • We could actually call this anything we wanted, but this allows us to "create" our own method called threeDigits( ) that we can use to give the templated number of digits following the decimal point.

 

  • now we use this new/temporary method to format the output before it is displayed using the method

threeDigits.format(area)

threeDigits.format(circumference)

We could create any other formatting for the number of digits in the same way.