0

Good Evening,

My code won't round off, and I don't know why! Please help!

import java.util.*;
import java.math.*;

public class Arithmetic {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int tipPercentage;  // tip percentage
        int taxPercentage;  // tax percentage
        int totalTip;       // total tip
        int totalTax;       // total tax
        double mealCost;    // original meal price
        double totalCost = 0.0; // total meal price

        // Write your calculation code here.
            mealCost = scan.nextDouble();
            tipPercentage = scan.nextInt ();
            taxPercentage = scan.nextInt();

            totalCost += mealCost;
            totalCost += mealCost*tipPercentage/100;
            totalCost += mealCost*taxPercentage/100;


        // cast the result of the rounding operation to an int and save it as totalCost 
            mealCost=(int)Math.round(totalCost);


        // Print your result
        System.out.println (" The total meal cost is " + totalCost + " dollars. ");
    }
}
cs95
  • 379,657
  • 97
  • 704
  • 746
  • Your question is a little vague, could you please edit it with a bit more detail on what the problem is – Barney Chambers Aug 03 '17 at 00:23
  • 1
    You have a typo, but the whole approach is fallacious. See my answer in the duplicate for why. If you want decimal places you must use a decimal radix, e.g. via `BigDecimal` or `DecimalFormat`, and in any case you should use `BigDecimal` for money, not floating-point. – user207421 Aug 03 '17 at 00:40

2 Answers2

0

You are appending totalCost to your printed string, when you are assigning a rounded value to mealCost. In other words,

mealCost=(int)Math.round(totalCost);

should become

totalCost=(int)Math.round(totalCost);

Additionally, you may want to add prompts before taking in input as well as perhaps reconsidering your rounding, unless you really want it to the nearest dollar.

CassOnMars
  • 6,153
  • 2
  • 32
  • 47
-1

mealCost is the variable that you are trying to round, whereas totalCost is the variable that you print out and display to the user.

System.out.println (" The total meal cost is " + totalCost + " dollars. ");

Also, when displaying cost it might be a good idea to round to the nearest hundredth. You can do this by saying something like

mealCost=Math.round(totalCost*100)/100.0

Finally, you should prompt the user, so they know what they are inputting in the console.

Luke Thistlethwaite
  • 428
  • 1
  • 4
  • 17