PDA

View Full Version : problem with rounding decimal places


inkedmn
08-29-2002, 09:09 PM
ok, here's my code:



public class MovieCashierApplication {

public static void main(String[] args) {

EasyIn easy = new EasyIn();
int movieNumber;
int adultTickets;
int childTickets;
float adultTicketPrice = (float)8.75;
float childTicketPrice = (float)5.50;
double TAX_RATE = 0.775;
double DISCOUNT_RATE = 0.10;
System.out.println("Hello! Welcome to the Movie Cashier.");
System.out.println("Which movie would you like to see?");
System.out.println("1) Martin Lawrence Live: Runteldat (R)");
System.out.println("2) The Last Kiss (R)");
System.out.println("3) The Crocodile Hunter: Collision Course (PG-13)");
System.out.println("4) Lilo & Stitch (PG)");
System.out.println("5) Happy Times (PG)");
System.out.println("6) Austin Powers in Goldmember (PG-13)");
System.out.println("7) The Kid Stays in the Picture (R)");
System.out.println("8) The Country Bears (G)");
System.out.println("9) Full Frontal (R)");
System.out.print("\nEnter the number of the movie you would like to see: ");
movieNumber = easy.readInt();
System.out.print("\nEnter the number of Adult tickets you'd like to buy: ");
adultTickets = easy.readInt();
System.out.print("\nEnter the number of Child tickets you'd like to buy: ");
childTickets = easy.readInt();
float adultTicketTotal = (float)adultTickets * adultTicketPrice;
float childTicketTotal = (float)childTickets * childTicketPrice;
float ticketSubtotal = adultTicketTotal + childTicketTotal;
double totalWithTax = ticketSubtotal + (ticketSubtotal * TAX_RATE);
System.out.println("Subotal with tax: $" + totalWithTax);
double discountAmount = ticketSubtotal * DISCOUNT_RATE;
System.out.println("Discount: $" + discountAmount);
double totalCost = totalWithTax - discountAmount;
System.out.println("Total Cost: $" + totalCost);
}

}


the EasyIn class that's being used is something our teacher gave us to use that allows for easy input of the different primitive types. i can put it up somewhere if someone wants to see it.

and here's the output:

D:\javaclass>java MovieCashierApplication
Hello! Welcome to the Movie Cashier.
Which movie would you like to see?
1) Martin Lawrence Live: Runteldat (R)
2) The Last Kiss (R)
3) The Crocodile Hunter: Collision Course (PG-13)
4) Lilo & Stitch (PG)
5) Happy Times (PG)
6) Austin Powers in Goldmember (PG-13)
7) The Kid Stays in the Picture (R)
8) The Country Bears (G)
9) Full Frontal (R)

Enter the number of the movie you would like to see: 9

Enter the number of Adult tickets you'd like to buy: 3

Enter the number of Child tickets you'd like to buy: 2
Subotal with tax: $66.11875
Discount: $3.725
Total Cost: $62.393750000000004


so...

a) does the code look ok? the casting seems a bit messy...

b) how do i round those dollar amounts to 2 decimal places?

thanks!!!

Strike
08-29-2002, 09:14 PM
http://java.sun.com/j2se/1.4/docs/api/java/util/Currency.html

Use the Currency class that comes with Java, silly ;)

Dru Lee Parsec
08-30-2002, 12:57 PM
The Currency class has only existed since 1.4 (the latest version) If your teacher is asking you to use an older JDK then you can also round off the extra decimal places using the java.text.DecimalFormat class

Example:


import java.text.*;

public class CurrencyFormat
{
public static void main(String args[])
{
double test1 = 123.4567890;
double test2 = 123345.56789;

DecimalFormat df = new DecimalFormat("###,###.##");
System.out.println("test1 is " + test1 + " formatted is " + df.format(test1));
System.out.println("test2 is " + test2 + " formatted is " + df.format(test2));

}
}


And here's the output:

test1 is 123.456789 formatted is 123.46
test2 is 123345.56789 formatted is 123,345.57

BTW, in the lines that look like this:

float adultTicketTotal = (float)adultTickets * adultTicketPrice;

You probably don't need the casting since a float times an int will still come out as a float. But it doesn't break anything by leaving it there. Also, casting it displays to any coder who comes after you to maintain the code what your intentions were. So, while not technically required, it's certainly not a mistake. IMHO

_underdog
08-30-2002, 01:29 PM
you could change all your dollar amounts to cents and stay away from floating point numbers if you are not concerned with fractions of cents and then format back as dollars and cents for output.

example
$5.15 --> 515

gufmn
08-30-2002, 01:49 PM
inkedmn: you'll prolly want to change TAX_RATE to equal 0.0775, not 0.775. 78% is kinda high ;)

kmj
08-30-2002, 02:44 PM
gufmn: clearly you don't live in NY :)

inkedmn
08-30-2002, 03:09 PM
ok, got this working

here's my code:




import java.util.*;
import java.text.NumberFormat;

public class MovieCashierApplication {

public static void main(String[] args) {

EasyIn easy = new EasyIn();
Locale aLocale = new Locale("us", "US");
NumberFormat nf = NumberFormat.getCurrencyInstance(aLocale);
int movieNumber;
int adultTickets;
int childTickets;
float adultTicketPrice = (float)8.75;
float childTicketPrice = (float)5.50;
double TAX_RATE = 0.0775;
double DISCOUNT_RATE = 0.10;
System.out.println("Hello! Welcome to the Movie Cashier.");
System.out.println("Which movie would you like to see?");
System.out.println("1) Martin Lawrence Live: Runteldat (R)");
System.out.println("2) The Last Kiss (R)");
System.out.println("3) The Crocodile Hunter: Collision Course (PG-13)");
System.out.println("4) Lilo & Stitch (PG)");
System.out.println("5) Happy Times (PG)");
System.out.println("6) Austin Powers in Goldmember (PG-13)");
System.out.println("7) The Kid Stays in the Picture (R)");
System.out.println("8) The Country Bears (G)");
System.out.println("9) Full Frontal (R)");
System.out.print("\nEnter the number of the movie you want to see: ");
movieNumber = easy.readInt();
System.out.print("\nEnter the number of Adult tickets you want: ");
adultTickets = easy.readInt();
System.out.print("\nEnter the number of Child tickets you want: ");
childTickets = easy.readInt();
float adultTicketTotal = (float)adultTickets * adultTicketPrice;
float childTicketTotal = (float)childTickets * childTicketPrice;
float ticketSubtotal = adultTicketTotal + childTicketTotal;
double totalWithTax = ticketSubtotal + (ticketSubtotal * TAX_RATE);
String formattedTotalWithTax = nf.format(totalWithTax);
System.out.println("Subotal with tax: " + formattedTotalWithTax);
double discountAmount = ticketSubtotal * DISCOUNT_RATE;
String formattedDiscountAmount = nf.format(discountAmount);
System.out.println("Discount: " + formattedDiscountAmount);
double totalCost = totalWithTax - discountAmount;
String formattedTotalCost = nf.format(totalCost);
System.out.println("Total Cost: " + formattedTotalCost);
}
}


thanks to Bradmont and Strike for helping me work the kinks out :)

oh, and thanks to Dru for his input also (even though i didn't see it because i'm lame) :)

AND THANK YOU TO KMJ FOR HIS MINIMAL ASSISTANCE.
;)

gufmn
08-30-2002, 03:57 PM
Here's mine for the same project:


import java.text.NumberFormat;
import java.util.*;

public class MovieCashier extends Object{
public static void main(String[] args) {

EasyIn easy = new EasyIn();
Locale myLocale = new Locale("us", "US");
NumberFormat nf = NumberFormat.getCurrencyInstance(myLocale);
float PRICE1 = (float)8.75;
float PRICE2 = (float)5.50;
double TAX_RATE = .0775;
double STUDENT_DISCOUNT = .10;
System.out.println("Hello! Welcome to the Movie Cashier\n\n");
System.out.println("1) Martin Lawerance Live: Runteldat (R)");
System.out.println("2) The Last Kiss (R)");
System.out.println("3) The Crocidile Hunter: Collision Course (PG-13)");
System.out.println("4) Lilo & Stitch (PG)");
System.out.println("5) Happy Times (PG)");
System.out.println("6) Austin Powers in Goldmember (PG-13)");
System.out.println("7) The Kid Stays in the Picture (R)");
System.out.println("8) The Country Bears (G)");
System.out.println("9) Full Frontal (R)");
System.out.println("\nPlease select a movie");
int MovieChoice = easy.readInt();
System.out.println("\nHow many Adult tickets would you like to purchase for movie? " + MovieChoice + "\n");
int NUM_ADULTS = easy.readInt();
System.out.println("\nHow many Children tickets would you like to purchase for movie? " + MovieChoice + "\n");
int NUM_KIDS = easy.readInt();
float SUBTOTAL = ((float)(NUM_ADULTS * PRICE1) + (float)(NUM_KIDS * PRICE2));
float TAXTOTAL = (float)(SUBTOTAL * TAX_RATE);
float TOTAL = (float)(SUBTOTAL + TAXTOTAL);
float DISCTOTAL =(float)(TOTAL * .9);
System.out.println("\nSubtotal is $" + nf.format(SUBTOTAL));
System.out.println("Sales tax is $" + nf.format(TAXTOTAL));
System.out.println("\nTotal ammount due is $" + nf.format(TOTAL));
System.out.println("\nStudent discounted price is $" + nf.format(DISCTOTAL));

}
}

inkedmn
08-30-2002, 04:46 PM
excuse me, but i believe i ordered the LARGE print statment...

;)

kmj
08-30-2002, 04:57 PM
inkedmn: Oh sure, don't thank me! :p

gufmn
08-30-2002, 05:07 PM
inkedmn == bitch :)

Dru Lee Parsec
08-30-2002, 05:15 PM
oh, and thanks to Dru for his input also (even though i didn't see it because i'm lame)

LOL!

Inkedmn did this because I busted his chops via AIM. :D

Thanks buddy. ;)

gufmn
08-30-2002, 05:24 PM
I cahnged it. Are you happy now Mr. inkedmn. You'd better be happy. DON'T PISS ME OFF!!!!! :D