| | |
Need a little help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2007
Posts: 23
Reputation:
Solved Threads: 0
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.
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.
Java Syntax (Toggle Plain Text)
// 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
Last edited by countrygirl1970; Sep 27th, 2008 at 1:42 am.
•
•
Join Date: Jan 2008
Posts: 3,827
Reputation:
Solved Threads: 501
•
•
•
•
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.
Java Syntax (Toggle Plain Text)
// 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:
Java Syntax (Toggle Plain Text)
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 "));•
•
Join Date: May 2008
Posts: 7
Reputation:
Solved Threads: 1
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
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
•
•
•
•
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
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
![]() |
Other Threads in the Java Forum
- Previous Thread: please enlighten me on what i need to do to get this right
- Next Thread: Help with implementing a Loader[Operating Systems]
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application array arrays automation binary bluetooth button character chat class classes client code component css csv database eclipse ee error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javaprojects jmf jni jpanel julia jvm key linked linux list loan loop map method methods mobile netbeans newbie objects oriented output panel phone print printf problem program programming project projects recursion replaydirector reporting researchinmotion robot rotatetext scanner screen se server service set size sms software sort sql string swing test threads time transfer tree ubuntu windows






