Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Help Calculating a tip

1. ## 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!

```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);
}
}```

3. ## Re: Help Calculating a tip

Let me guess: Divide the smaller number by the bigger one and multiply by one hundred?

4. ## Re: Help Calculating a tip

"15 percent" means literally, 15 / 100. 15 percent, or '%', of something is

something * ( 15 / 100 )

5. ## 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.

6. ## 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.

7. ## The Following User Says Thank You to mstabosz For This Useful Post:

Sephyncloud (September 13th, 2013)

8. ## 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!

9. ## Re: Help Calculating a tip

Do a search on formatting java output

10. ## Re: Help Calculating a tip

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.

11. ## Re: Help Calculating a tip

And yet another class called DecimalFormat.