Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help with limiting JOptionPane output to 2 decimal places

807599May 7 2007 — edited May 7 2007
I don't seem to be able to figure out how to specify 2 decimal points in my output to a JOptionPane. I know how to do this in a standard program with no gui interface but I have been unsuccessful in my attempts with JOptionPane. I have included my code below and any assistance at all would be appreciated.

import javax.swing.JOptionPane;

public class Vehicleanalysis2
{
public static void main( String args[] )
{
String januaryRepairs =
JOptionPane.showInputDialog( "Enter January's repair costs:" );
String februaryRepairs =
JOptionPane.showInputDialog( "Enter February's repair costs:" );
String marchRepairs =
JOptionPane.showInputDialog( "Enter March's repair costs:" );
String aprilRepairs =
JOptionPane.showInputDialog( "Enter April's repair costs:" );
String mayRepairs =
JOptionPane.showInputDialog( "Enter May's repair costs:" );
String juneRepairs =
JOptionPane.showInputDialog( "Enter June's repair costs:" );
String julyRepairs =
JOptionPane.showInputDialog( "Enter July's repair costs:" );
String augustRepairs =
JOptionPane.showInputDialog( "Enter August's repair costs:" );
String septemberRepairs =
JOptionPane.showInputDialog( "Enter September's repair costs:" );
String octoberRepairs =
JOptionPane.showInputDialog( "Enter October's repair costs:" );
String novemberRepairs =
JOptionPane.showInputDialog( "Enter November's repair costs:" );
String decemberRepairs =
JOptionPane.showInputDialog( "Enter December's repair costs:" );



double january = Double.parseDouble(januaryRepairs );
double february = Double.parseDouble(februaryRepairs );
double march = Double.parseDouble(marchRepairs );
double april = Double.parseDouble(aprilRepairs );
double may = Double.parseDouble(mayRepairs );
double june = Double.parseDouble(juneRepairs );
double july = Double.parseDouble(julyRepairs );
double august = Double.parseDouble(augustRepairs );
double september = Double.parseDouble(septemberRepairs );
double october = Double.parseDouble(octoberRepairs );
double november = Double.parseDouble(novemberRepairs );
double december = Double.parseDouble(decemberRepairs );

double annualrepaircost = january + february + march + april + may + june + july + august + september + october + november + december;

double payment = annualrepaircost / 12;
if (annualrepaircost <= 3600 )

JOptionPane.showMessageDialog(null, " Keep your car.\n Your monthly repair\n costs only average\n out to be: " + payment + "\n Your annual repair costs\n this year\n were: " + annualrepaircost, " Recommendation", JOptionPane.PLAIN_MESSAGE );
else

JOptionPane.showMessageDialog(null, " You should consider purchasing a new\n car if you can get a payment\n for less than: " + payment + "\n Your annual repair costs\n this year\n were: " + annualrepaircost,
" Recommendation: ", JOptionPane.PLAIN_MESSAGE );
}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 4 2007
Added on May 7 2007
2 comments
3,034 views