Skip to Main Content

Java Programming

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!

FORMATTING AN OUTPUT (DECIMAL FORMAT)

807589Jul 2 2008 — edited Jul 2 2008
great day to everyone.

i have just finished creating a program which will compute 5 numbers with decimal points.

our professor asked us to modify. i have no idea how it will diplay an output with only 2 decimal places (as it will automatically round off the output) even if you enter a number with infinite decimals.

here's the program and it works:

import javax.swing.*;
public class Five {
public static void main ( String args[] )
{
String firstNumber, secondNumber, thirdNumber, fourthNumber, fifthNumber;
double num1=0, num2=0, num3=0, num4=0, num5=0, sum;
firstNumber=JOptionPane.showInputDialog("Enter first number");
secondNumber=JOptionPane.showInputDialog("Enter second number");
thirdNumber=JOptionPane.showInputDialog("Enter third number");
fourthNumber=JOptionPane.showInputDialog("Enter fourth number");
fitthNumber=JOptionPane.showInputDialog("Enter fifth number");

num1=Dopuble.parseDouble(firstNumber);
num2=Dopuble.parseDouble(secondNumber);
num3=Dopuble.parseDouble(thirdNumber);
num4=Dopuble.parseDouble(fourthNumber);
num5=Dopuble.parseDouble(fifthNumber);

sum=num1+num2+num3+num4+num5;

JOptionPane.showInputDialog(null,"The sum is"+sum, "Results",JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}


any help from anyone will be gladly appreciated.

thanks very much in advanced. god bless everyone.

love, peace, equality.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 30 2008
Added on Jul 2 2008
1 comment
247 views