0

I'm trying to write a simple tip calculator in Python, but I need it to always output two decimal places. With some whole numbers, like 100, it will only give one decimal point, what should I do?

Here is my code:

print("Only input numbers, do not input dollar signs or letters")
subtotalprice = eval(input("What is the bill total?: "))



percent10 = subtotalprice * 0.1
percent15 = subtotalprice * 0.15
percent20 = subtotalprice * 0.2

percent10_2 = str(round(percent10, 2))
percent15_2 = str(round(percent15, 2))
percent20_2 = str(round(percent20, 2))


percent10total = subtotalprice * 1.10
percent15total = subtotalprice * 1.15
percent20total = subtotalprice * 1.20


percent10total_2 = str(round(percent10total, 2))
percent15total_2 = str(round(percent15total, 2))
percent20total_2 = str(round(percent20total, 2))

print("If you want to leave a 10% tip, the tip comes to", percent10_2, "With 
the 
subtotal coming to", percent10total_2,)

print("If you want to leave a 15% tip, the tip comes to", percent15_2, "With 
the 
subtotal coming to", percent15total_2,)

print("If you want to leave a 20% tip, the tip comes to", percent20_2, "With 
the subtotal coming to", percent20total_2,)

input("Press Enter to Close")
cvaska
  • 3
  • 1

1 Answers1

0

Try to use string formatting

print("tip: %.2f" % percent)

where percent is that floating point variable to print.

Jon Deaton
  • 3,943
  • 6
  • 28
  • 41