Using a User Defined Method to Convert Temperatures
Introduction.
Now we will slightly modify what we did in the last webpage to format the
output so that it displays only two decimal places. We will do this
by importing the DecimalFormat classes of the .text package and creating
an additional user defined method. The actual changes to the
previous code will be quite small. You should call this JSP page sticky_temperature_formatted.jsp. |
<html> <head> <title>A Sticky Form for Converting Temperatures</title> </head> <body bgcolor = "003344" text="cccccc"> <%@ page import = "java.text.DecimalFormat" %> <%
}
} String twoDigitString(double input) |
After copying this code and uploading it and accessing it on your web you should see something like the following after entering the initial input and submitting the form for computation. |
Notice how we added the page import <%@ page import = "java.text.DecimalFormat" %> We also added the method, twoDigitString( ) that returns a String to format a number with two decimal places. |