Hello,

I am trying to get my cost, salestax, totalamount to come out in the output dialogbox to have only two decimals places. I know how to do on the printf but not on the dialogbox. Please help. Here in my complete code.

// Displaying multiple strings
import javax.swing.*;                // to use JOptionPane.show InputDialog box

public class Assignment3 
{
	public static void main( String args[] )
	{
		final double taxes = 0.0825;     // rate for taxes calculation (constant)
			
		int 
		     moreInt = 1,                // loop control variable
		     days = 0;                   // the number of days wanted
		     
		double
	                     totalPrice = 0,             // the sum of calculated rental days
		     salesTax = 0,               // the calculated sales tax
		     cost = 0,                   // fixed cost on the amount of rental days
		     totalAmount = 0;            // the total amount for rental days
			       
		String numberDays;               // number of days
		String output = "";              // total rental message
		String input = "";               // users input	
		
						
// process unknown number of days, boolean controled loop
    while ( moreInt == 1 ) {
         numberDays = JOptionPane.showInputDialog(
                "Enter the number of rental days" );
                
        // convert numbers from type String       
              days = Integer.parseInt( numberDays ); 
              
         
         // to determine the cost on the rental days
              	if( days >= 1 && days <= 3)
              	{
   		             cost = 35.00;
		        }
		        else if( days >= 4 && days <= 7 )
		        {
         		     cost = 30.00;
		        }
		        else if( days >= 8)
		        {
			         cost = 26.00;
		        }
                
                
         // calculations
              totalAmount = days * cost;
              salesTax = totalAmount * taxes;
              totalPrice = totalAmount + salesTax;
              
         
         // message total output
         JOptionPane.showMessageDialog(null, ("Car Rental Statement" +
         	                          "\n------------------------------" +
         	                          "\n" + days + " days $" + cost + " per day " + totalAmount +
                                      "\nSales taxes @ 8.25% $ " + salesTax + 
                                      "\nTotal Price $ " + totalPrice + 
                                      " \n \n ")); 
                                      	
         // display the "Thank you" message
              if (totalPrice < 100)
                  output = "Thank you - We hope to see you again!";
              else if (totalPrice > 100)
                  output = "Thank you - We appreciate your business!";
                  
         // display message total
         JOptionPane.showMessageDialog( null, output );
              
         // display the "Another Rental" message                  
         input = JOptionPane.showInputDialog(
                  "Another rental?\n   1 - yes\n   2 - no\n" );
                    
         moreInt = Integer.parseInt( input );
         
      }  //while loop ends
      
      
		
	}  // end method main
	
   
}  // end method Assignment3

Recommended Answers

All 5 Replies

Hello,

I am trying to get my cost, salestax, totalamount to come out in the output dialogbox to have only two decimals places. I know how to do on the printf but not on the dialogbox. Please help. Here in my complete code.

// Displaying multiple strings
import javax.swing.*;                // to use JOptionPane.show InputDialog box

public class Assignment3 
{
	public static void main( String args[] )
	{
		final double taxes = 0.0825;     // rate for taxes calculation (constant)
			
		int 
		     moreInt = 1,                // loop control variable
		     days = 0;                   // the number of days wanted
		     
		double
	                     totalPrice = 0,             // the sum of calculated rental days
		     salesTax = 0,               // the calculated sales tax
		     cost = 0,                   // fixed cost on the amount of rental days
		     totalAmount = 0;            // the total amount for rental days
			       
		String numberDays;               // number of days
		String output = "";              // total rental message
		String input = "";               // users input	
		
						
// process unknown number of days, boolean controled loop
    while ( moreInt == 1 ) {
         numberDays = JOptionPane.showInputDialog(
                "Enter the number of rental days" );
                
        // convert numbers from type String       
              days = Integer.parseInt( numberDays ); 
              
         
         // to determine the cost on the rental days
              	if( days >= 1 && days <= 3)
              	{
   		             cost = 35.00;
		        }
		        else if( days >= 4 && days <= 7 )
		        {
         		     cost = 30.00;
		        }
		        else if( days >= 8)
		        {
			         cost = 26.00;
		        }
                
                
         // calculations
              totalAmount = days * cost;
              salesTax = totalAmount * taxes;
              totalPrice = totalAmount + salesTax;
              
         
         // message total output
         JOptionPane.showMessageDialog(null, ("Car Rental Statement" +
         	                          "\n------------------------------" +
         	                          "\n" + days + " days $" + cost + " per day " + totalAmount +
                                      "\nSales taxes @ 8.25% $ " + salesTax + 
                                      "\nTotal Price $ " + totalPrice + 
                                      " \n \n ")); 
                                      	
         // display the "Thank you" message
              if (totalPrice < 100)
                  output = "Thank you - We hope to see you again!";
              else if (totalPrice > 100)
                  output = "Thank you - We appreciate your business!";
                  
         // display message total
         JOptionPane.showMessageDialog( null, output );
              
         // display the "Another Rental" message                  
         input = JOptionPane.showInputDialog(
                  "Another rental?\n   1 - yes\n   2 - no\n" );
                    
         moreInt = Integer.parseInt( input );
         
      }  //while loop ends
      
      
		
	}  // end method main
	
   
}  // end method Assignment3

use DecimalFormat

Here's an example:

http://www.dreamincode.net/code/snippet399.htm

Just set up a DecimalFormat object as in the example:

DecimalFormat money = new DecimalFormat("$0.00");

and put it in your code (see red below):

JOptionPane.showMessageDialog(null, ("Car Rental Statement" +
"\n------------------------------" +
"\n" + days + " days $" + cost + " per day " + totalAmount +
"\nSales taxes @ 8.25% $ " + money.format (salesTax) + 
"\nTotal Price $ " + totalPrice + 
" \n \n "));

Thank you for your help. I was able to fix my program.

countrygirl1970

hi countrygirl i am giving you the right method to do it

import javax.swing.*;                // to use JOptionPane.show InputDialog box

public class Assignment3 
{
    public static void main( String args[] )
    {
        final double taxes = 0.0825;     // rate for taxes calculation (constant)

        int 
             moreInt = 1,                // loop control variable
             days = 0,                   // the number of days wanted
             Ta;
        double
                         totalPrice = 0,             // the sum of calculated rental days
             salesTax = 0,               // the calculated sales tax
             cost = 0,                   // fixed cost on the amount of rental days
             totalAmount = 0,           // the total amount for rental days
        tr;       
        String numberDays;               // number of days
        String output = "";              // total rental message
        String input = "";               // users input 


// process unknown number of days, boolean controled loop
    while ( moreInt == 1 ) {
         numberDays = JOptionPane.showInputDialog(
                "Enter the number of rental days" );

        // convert numbers from type String       
              days = Integer.parseInt( numberDays ); 


         // to determine the cost on the rental days
                if( days >= 1 && days <= 3)
                {
                     cost = 35.00;
                }
                else if( days >= 4 && days <= 7 )
                {
                     cost = 30.00;
                }
                else if( days >= 8)
                {
                     cost = 26.00;
                }


         // calculations
              totalAmount = days * cost;
              salesTax = totalAmount * taxes;
              totalPrice = totalAmount + salesTax;
tr = Math.IEEEremainder(totalPrice,1);
tr = tr*100;
tr = Math.rint(tr);
tr= tr*.01;
totalPrice = Math.rint(totalPrice) + tr;




         // message total output
         JOptionPane.showMessageDialog(null, ("Car Rental Statement" +
                                      "\n------------------------------" +
                                      "\n" + days + " days $" + cost + " per day " + totalAmount +
                                      "\nSales taxes @ 8.25% $ " + salesTax + 
                                      "\nTotal Price $ " + totalPrice + 
                                      " \n \n ")); 

         // display the "Thank you" message
              if (totalPrice < 100)
                  output = "Thank you - We hope to see you again!";
              else if (totalPrice > 100)
                  output = "Thank you - We appreciate your business!";

         // display message total
         JOptionPane.showMessageDialog( null, output );

         // display the "Another Rental" message                  
         input = JOptionPane.showInputDialog(
                  "Another rental?\n   1 - yes\n   2 - no\n" );

         moreInt = Integer.parseInt( input );

      }  //while loop ends



    }  // end method main


}  // end method Assignment3

the wisdom of the ignorant????

  1. the problem was fixed the day before you posted this

  2. you are just formatting the data, with the chance you'll either loose data or have to write lots more code. take a look at the sollution provided by VernonDozier, and you'll see that your sollution sure is not the best in the world

Thank you all for the help. I was able to fix the problem and submitted the program. I received an A.

Thank you again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.