MessageDialog working with DecimalFormat?
Code java:
package internetserviceproviderpart2;
import javax.swing.JOptionPane;
/**
*
* @author Home
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String inputString;
char packageLetter;
int hoursUsed;
int additionalHours = 0;
double totalFee = 0;
double savingAB = 0;
double savingAC = 0;
double savingBC = 0;
inputString = JOptionPane.showInputDialog("Enter the letter of the " +
"package you purchased (either A, B, or C.)");
inputString = inputString.toUpperCase();
packageLetter = inputString.charAt(0);
inputString = JOptionPane.showInputDialog("Enter the number of hours " +
"you used.");
hoursUsed = Integer.parseInt(inputString);
if (packageLetter=='A' && hoursUsed>10)
{
additionalHours = hoursUsed - 10;
totalFee = 9.95 + (additionalHours * 2.00);
savingAB = totalFee - 13.95;
savingAC = totalFee - 19.95;
JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
totalFee + ".");
if(packageLetter == 'A' && hoursUsed > 10 && totalFee > 19.95 && +
savingAB>0 && savingAC>0)
JOptionPane.showMessageDialog(null,"You would save $" + savingAB +
" if you purchase Package B.");
JOptionPane.showMessageDialog(null,"You would save $" + savingAC +
" if you purchase Package C.");
}
else if (packageLetter=='A' && hoursUsed<=10)
JOptionPane.showMessageDialog(null,"Your monthly bill is $9.95.");
else if (packageLetter=='B' && hoursUsed>20)
{
additionalHours = hoursUsed - 20;
totalFee = 13.95 + (additionalHours * 1.00);
savingBC = totalFee - 19.95;
JOptionPane.showMessageDialog(null,"Your monthly bill is $" +
totalFee + ".");
if (packageLetter == 'B' && hoursUsed > 20 && totalFee > 19.95 && +
savingBC>0)
JOptionPane.showMessageDialog(null,"You would save $" + savingBC +
" if you purchase Package C.");
}
else if(packageLetter=='B' && hoursUsed<=20)
JOptionPane.showMessageDialog(null,"Your monthly bill is $13.95.");
else if (packageLetter=='C')
JOptionPane.showMessageDialog(null,"Your monthly bill is $19.95.");
else
JOptionPane.showMessageDialog(null,"Enter either A, B, or C.");
System.exit(0);
}
}
Is it possible to use DecimalFormat while printing it out in a MessageDialog?
e.g. I entered 'A' and '77' in this code and one of the MessageDialogs print out $123.99999999999
I want to know if I can make that $123.999999999999999999 into $123.99.
Re: MessageDialog working with DecimalFormat?
Read the API for String and look for a method that returns a formatted version of the String.
And shouldn't 123.999999999999999999 be represented as 124.00?
db
Re: MessageDialog working with DecimalFormat?
you can use a DecimalFormat:
DecimalFormat df = new DecimalFormat("0.00");
String formatedNum = df.format(savngsAc);