1

This is my first time working with Python. I'm trying to figure out how to round decimals in the simplest way possible.

print("\nTip Calculator")

costMeal = float(input("Cost of Meal:"))

tipPrct = .20
print("Tip Percent: 20%")

tip = costMeal * tipPrct

print("Tip Amount: " + str(tip))

total = costMeal + tip
print("Total Amount: " + str(total))

I need it to look like this image.

Matthew Anderson
  • 338
  • 1
  • 13
hts95
  • 103
  • 1
  • 1
  • 9
  • So [this](https://stackoverflow.com/questions/9232256/round-up-to-second-decimal-place-in-python/9232295#9232295) isn't helping? – Austin Jul 12 '19 at 17:23
  • Have you tried `round(some_value, 1)`? – duhaime Jul 12 '19 at 17:23
  • There is a function called `round()`, but you could also just use formatting for how you display the number. – John Coleman Jul 12 '19 at 17:23
  • I guess I'm just not understanding how to use round() with what I'm doing.. would I put it on it's own like: round(total, 2)? Because when I try that it doesn't do anything to the numbers – hts95 Jul 12 '19 at 17:25
  • Ah I figured it out! Thank you all for your answers – hts95 Jul 12 '19 at 17:36
  • @hts95, glad you figured it out. So people in the future can get a good answer, make sure you accept one of the answers. – Matthew Anderson Jul 12 '19 at 17:38

1 Answers1

2

You should use Python's built-in round function.

Syntax of round():

round(number, number of digits)

Parameters of round():

..1) number - number to be rounded
..2) number of digits (Optional) - number of digits 
     up to which the given number is to be rounded.
     If not provided, will round to integer.

Therefore, you should try code more like:

print("\nTip Calculator")

costMeal = float(input("Cost of Meal: "))

tipPrct = .20
print("Tip Percent: 20%")

tip = costMeal * tipPrct
tip = round(tip, 2) ## new line

print("Tip Amount: " + str(tip))

total = costMeal + tip
print("Total Amount: " + str(total))
Matthew Anderson
  • 338
  • 1
  • 13