Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: MessageDialog working with DecimalFormat?

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default MessageDialog working with DecimalFormat?

    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.


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default 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

  3. #3
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: MessageDialog working with DecimalFormat?

    you can use a DecimalFormat:

    DecimalFormat df = new DecimalFormat("0.00");

    String formatedNum = df.format(savngsAc);

Similar Threads

  1. [SOLVED] Buttons not working..
    By khms in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 2nd, 2010, 06:01 PM
  2. [SOLVED] CoreJavaExample Not Working
    By prasana in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 12th, 2010, 11:23 AM
  3. Working with threads
    By tccool in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 12th, 2010, 10:21 AM
  4. Cannot seem to get this working
    By OttawaGuy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 28th, 2010, 03:41 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM