-1

The total bill for the meal (the meal charge plus the tax and the tip)

public class menu {

public static void main(String[] args) 
{
    double burgers; 
    double soda; 
    double meal;
    double tax = 0.0825;
    double taxAmount;
    double totalWithTax;
    double tipRate = 0.15;
    double tipAmount;
    double totalBill;

    //charge and tax 

    burgers = 5 * 6.95;
    soda = 4 * 1.75;
    meal = burgers + soda;
    taxAmount = meal*tax;
    totalWithTax = meal + tax;
    tipAmount = totalWithTax * tipRate;
    totalBill = meal + taxAmount + tipAmount;


    System.out.println("Total meal charge $ "+ meal);
    System.out.println("Tax amount "+ taxAmount);
    System.out.println("Tip amount " + tipAmount);
    System.out.println("Total bill " + totalBill);
}

}

output:

Total meal charge $ 41.75

Tax amount 3.444375 <=== I need truncated to 3.44

Tip amount 6.274875000000001 <=== I need this truncated to 6.78

Total bill 51.46925

user3905353
  • 77
  • 2
  • 12
  • 1
    Why are you multiplying by 100, casting to int and then dividing by 100 again? Your tax amount multiplier is bounded between 0 ~ 100 rather than 0 ~ 1, you should be dividing by 100 in the end. If you're using the multiple by 100 to solve rounding issues, your final divisor should be 100,000 and not 100. – initramfs Feb 07 '15 at 06:45
  • 8.25% tax means the tax should be sub_total * 0.0825. Also it is preferable to use Math.round instead of casting to an int (so that >= 0.5 will round up). – kylewm Feb 07 '15 at 06:52
  • I When I use Math.round I get 344.0. – user3905353 Feb 07 '15 at 12:22

3 Answers3

0

Instead of

System.out.println("Tax amount "+ (int) (tax_amount * 100) /100);//100/100 doesnt makes sense here and you need decimal number so dont typecast to int which will remove the decimal part.

Use

System.out.printf("Tax amount %.2f", tax_amount / 100.0);

After OP's edited question:

Change these:

System.out.println("Total meal charge $ "+ meal);
System.out.println("Tax amount "+ taxAmount);
System.out.println("Tip amount " + tipAmount);
System.out.println("Total bill " + totalBill);

To:

 System.out.printf("Total meal charge $ %.2f\n", meal);
 System.out.printf("Tax amount %.2f\n", taxAmount);
 System.out.printf("Tip amount %.2f\n", tipAmount);
 System.out.printf("Total bill %.2f\n", totalBill);

Output is:

Total meal charge $ 41.75 
Tax amount 3.44 
Tip amount 6.27 
Total bill 51.47 
SMA
  • 36,381
  • 8
  • 49
  • 73
  • When I use your solution i get long string of number at the end. – user3905353 Feb 07 '15 at 12:23
  • can you pass on example what you are trying? – SMA Feb 07 '15 at 12:24
  • I calculating a restaurant bill with tax. – user3905353 Feb 07 '15 at 15:57
  • Total meal charge $ 41.75 Tax amount 3.444375 Tip amount 6.274875000000001 Total bill 51.46925 – user3905353 Feb 07 '15 at 15:58
  • `tax_amount = 344.4375; System.out.printf("Tax amount %.2f", tax_amount / 100.0);` gives me output as `Total meal charge $ 41.75 Tax amount 3.44` – SMA Feb 07 '15 at 16:06
  • Thats the output I get when I run the program. My tax amount = 0.0825 * 41.75. I get 344.4375 as result of this calculation. I need the result to be 3.44 as an out put. This is what I get from your last solution Tax amount 0.03Tip amount 6.274875000000001. Thanks – user3905353 Feb 07 '15 at 16:30
  • This solution worked. But, cannot get on a new line for each statement. Everything is on one line now. – user3905353 Feb 07 '15 at 17:18
  • See my edited answer. You need a new line character at the end of string. – SMA Feb 07 '15 at 17:22
0

Use the double format. You can round your doubles to any place you need, in this case 2.

double taxAmmount;
double tipAmount;
double totalBill;

DecimalFormat dFormat = new DecimalFormat("#.##");

//get the tax ammount
taxAmount = meal*tax;

//round to two decimal places.
taxAmmount = Double.parseDouble(dFormat(taxAmmount));

//for the tipAmmount and totalBill do the same thing.
//do caclulations for each.
//then format.
tipAmmount = Double.parseDouble(dFormat(tipAmmount));
totalBill = Double.parseDouble(dFormat(totalBill));
MGHandy
  • 363
  • 1
  • 3
  • 11
0

I wouldn't recommend doubles for these types of calculations, unless you're 100% up to date on what they can and can't do and want to jump through a lot of hoops to make sure your calculations won't go off, it's much easier to use BigDecimals. Check EJP's answer to this question: https://stackoverflow.com/a/12684082/144578

Or for a good (off-site) read, check http://floating-point-gui.de/

Community
  • 1
  • 1
Sebastiaan van den Broek
  • 5,818
  • 7
  • 40
  • 73