Help Calculating a tip

• September 13th, 2013, 03:24 PM
Sephyncloud
Help Calculating a tip
Hello everyone! Having a bit of trouble with the math on this one. When I run the program it doesn't read the gratuity as a percentage, which is why it's showing up as a whole number. I need this program to basically output the tip amount by itself, and then the grand total. Of course allowing the user to input whatever the rate of gratuity is. Any help/advice is appreciated! ~o) :)>-

Code Java:

```package calculatingtip; import java.util.Scanner; public class CalculatingTip {   public static void main(String[] args) { Scanner input=new Scanner(System.in); double billBeforeTip; double tipRate; double tipAmount; double grandTotal;   System.out.println("Enter Bill Amount"); billBeforeTip = input.nextDouble(); System.out.println("Enter Rate of Gratuity"); tipRate = input.nextDouble(); System.out.println("The Amount before the tip is: " + billBeforeTip + " The Gratuity rate is " + tipRate + "%"); tipAmount = billBeforeTip * tipRate; grandTotal = tipAmount + billBeforeTip; System.out.println("The Grand Total comes to " + grandTotal + "The Gratuity is: " + tipAmount); } }```
• September 13th, 2013, 03:26 PM
KevinWorkman
Re: Help Calculating a tip
• September 13th, 2013, 03:35 PM
Sephyncloud
Re: Help Calculating a tip
Let me guess: Divide the smaller number by the bigger one and multiply by one hundred?
• September 13th, 2013, 03:40 PM
GregBrannon
Re: Help Calculating a tip
"15 percent" means literally, 15 / 100. 15 percent, or '%', of something is

something * ( 15 / 100 )
• September 13th, 2013, 03:58 PM
Sephyncloud
Re: Help Calculating a tip
I'm still having issues implementing this into my code. I see how I would need to get the number and divide it by 100, but I'm not sure which variables I need to include, and how the percentage can be chosen by the user. I appreciate the help so far.
• September 13th, 2013, 04:51 PM
mstabosz
Re: Help Calculating a tip
The standard tip rate isn't 15%, it's 0.15 when you actually calculate. So you would want to update whichever variable represents the tip amount to reflect that.
• September 13th, 2013, 05:16 PM
Sephyncloud
Re: Help Calculating a tip
Thanks for the help everyone, I got my code working great now! Just a quick question though. When the tip amount is displayed at the end of my new program, it shows up as \$1.5 (When 15% is the tip amount). Is there a way to make it show up as \$1.50? I am mostly inquiring about the aesthetics of the output, not necessarily the function. Thanks again! :)>-
• September 13th, 2013, 10:00 PM
jps
Re: Help Calculating a tip
Do a search on formatting java output
• September 14th, 2013, 03:54 PM
mstabosz
Re: Help Calculating a tip
Quote:

hanks for the help everyone, I got my code working great now! Just a quick question though. When the tip amount is displayed at the end of my new program, it shows up as \$1.5 (When 15% is the tip amount). Is there a way to make it show up as \$1.50? I am mostly inquiring about the aesthetics of the output, not necessarily the function.
There's a function called printf used for formatting console output.
• September 16th, 2013, 07:58 AM
KevinWorkman
Re: Help Calculating a tip
And yet another class called DecimalFormat.