Why is it that when I use Math.round() to round a variable within a Java program, the numbers dont get rounded and remain the same?

Viewed 20

Here is the code below. Im trying to round the total costs to paint the room to TWO DECIMALS and the Gallon to a whole number that cant be decimal using Math.round(). However when I use Math.round() as part of my code, the total cost and gallon variable output a number that isnt rounded and remains with many decimals. Here is my code below:

Scanner User = new Scanner(System.in);

    double areaTotal;
    double doorArea;
    double windowArea;
    double RoomArea;
    double gallonsNeeded;
    double GallonAmount;
    double cost;
    
    
    System.out.println("This program determines the amount of paint needed to paint a room");
    System.out.println("with a door and 2 windows, as well as the cost.");
    System.out.println(" ");
    
    //Room dimensions from the user
    System.out.println("Enter the length of the room (m):");
    double length = User.nextDouble();
    System.out.println(" ");
    System.out.println("Enter the height of the room (m):");
    double height = User.nextDouble();
    System.out.println(" ");
    System.out.println("Enter the width of the room (m):");
    double width = User.nextDouble();
    System.out.println(" ");
    
    System.out.println("You will also be prompted to enter the dimension of the windows and door!");
    System.out.println(" ");
    
    System.out.println("Enter the width of the door (m):");
    double doorWidth = User.nextDouble();
    System.out.println(" ");
    System.out.println("Enter the length of the door (m):");
    double doorHeight = User.nextDouble();
    System.out.println(" ");
    
    System.out.println("Enter the dimensions the window!");
    System.out.println(" ");
    System.out.println("What is the length of the window (m):");
    double windowLength = User.nextDouble();
    System.out.println(" ");
    System.out.println("Enter the width of the window (m):");
    double windowWidth = User.nextDouble();
    System.out.println(" ");
    
    RoomArea = 2*length*height+(2*width*height);
    doorArea = windowLength*windowWidth;
    windowArea = 2*windowWidth*windowLength;
    areaTotal = RoomArea - (2*windowArea + doorArea);
    GallonAmount = areaTotal/40;
    cost = GallonAmount * 51.75; 
    
    Math.round(cost*100/100);
    Math.round(GallonAmount);
    
    System.out.println("Your room's total area is " + areaTotal + "m².");
    System.out.println("The total cost to paint the room is $" +cost+ " and " + GallonAmount + " gallons to paint the room!");
0 Answers
Related