Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Limiting Decimals
#1
I am fresh into a programming fundamentals course, so yes, I have looked at python documentation on how to do what I am asking, I just don't have the coding experience to implement it. Super simple "express checkout" code, however, due to how python does math, I get more than 2 decimal places for the sales tax and total outputs. I am just trying to figure out how to stop the results at 2 decimal places as no one needs to see 15 decimal places on their receipt.

print("Welcome to the Express Lane!")
price1 = float(input("What is the price of the Frst item: $"))
price2 = float(input("What is the price of the second item: $"))
price3 = float(input("What is the price of the third item: $"))
price4 = float(input("What is the price of the fourth item: $"))
price5 = float(input("What is the price of the last item: $"))
subtotal = float(price1 + price2 + price3 + price4 + price5)
sales_tax = subtotal * 0.06
total = subtotal + sales_tax
print("Subtotal: $", subtotal)
print("Sales Tax: $", sales_tax)
print("-----------------------------")
print("Total $", total)
OUTPUT:
Welcome to the Express Lane!
What is the price of the Frst item: $4.2
What is the price of the second item: $6.35
What is the price of the third item: $4
What is the price of the fourth item: $4
What is the price of the last item: $4
Subtotal: $ 22.55
Sales Tax: $ 1.353
-----------------------------
Total $ 23.903000000000002

Process finished with exit code 0

Had a PyCharm screenshot put can't post it.
Reply
#2
use format statement.
print('Subtotal: ${0:.2f}'.format(subtotal))
Reply
#3
Thank you! You have done 2 things for me.

1. I finished the program.
2. I have a better understanding of how to format this particular bit a code. So thank you again.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to use range with decimals KameronG 7 3,234 Feb-08-2019, 07:57 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020