Hi, I was assigned by my teacher to create a simple calculation program about movie tickets that are sold in a local movie theater. So, I created the coding, and it turned out perfect. Here it is:

import java.util.Scanner;

public class MovieTheater
    public static void main(String[] args)

    String movieName;
    double adultPrice, childPrice, totalAdultPrice, totalChildPrice, grossAmount, percentageDonation, amountDonated, netSale;
    int adultNumber, childNumber, totalTickets;

    Scanner keyboard = new Scanner(System.in);
    System.out.println(" Enter the Movie Title: ");
    movieName = keyboard.nextLine();
    System.out.println(" Adult ticket price per person: ");
    adultPrice = keyboard.nextDouble();
    System.out.println(" Child ticket price per person: ");
    childPrice = keyboard.nextDouble();
    System.out.println(" Number of adult tickets sold: ");
    adultNumber = keyboard.nextInt();
    System.out.println(" Number of child tickets sold: ");
    childNumber = keyboard.nextInt();
    System.out.println(" Percentage of gross amount to be donated: ");
    percentageDonation = keyboard.nextDouble();

    totalTickets = adultNumber + childNumber;
    totalAdultPrice = adultPrice * adultNumber;
    totalChildPrice = childPrice * childNumber;
    grossAmount = totalAdultPrice + totalChildPrice; 
    amountDonated = (percentageDonation/100) * grossAmount;
    netSale = grossAmount - amountDonated;

    System.out.println(" Movie Name: " + movieName);
    System.out.println(" Number of tickets sold: " + totalTickets);
    System.out.println(" Gross Amount: " + " $ " + grossAmount);
    System.out.println(" Percentage of the gross amount to be donated: " + percentageDonation);
    System.out.println(" Amount Donated: " + " $ " + amountDonated);
    System.out.println(" Net Sale: " + " $ " + netSale);    

BUT, the outputs "Gross Amount", "Percentage", "Amount Donated" and "Net Sale" are not in the same decimal places as you can see in the output I got here:

 Enter the Movie Title: 
 Adult ticket price per person: 
 Child ticket price per person: 
 Number of adult tickets sold: 
 Number of child tickets sold: 
 Percentage of gross amount to be donated: 
 Movie Name: Anastasia
 Number of tickets sold: 112
 Gross Amount:  $ 826.0
 Percentage of the gross amount to be donated: 20.0
 Amount Donated:  $ 165.20000000000002
 Net Sale:  $ 660.8

I WANT THEM TO BE IN TWO DECIMAL PLACES ONLY. can someone help show me where to add/correct? Thanks in advance!

4 Years
Discussion Span
Last Post by UNDER-18 FG
Featured Replies
  • Instead of the println method, use printf printf allows you to specify formats for how each variable is printed. Have a look at http://www.homeandlearn.co.uk/java/java_formatted_strings.html Read More

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.