Python Forum
Limiting Decimals - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Limiting Decimals (/thread-3858.html)



Limiting Decimals - FZMoto - Jul-01-2017

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.


RE: Limiting Decimals - Larz60+ - Jul-01-2017

use format statement.
print('Subtotal: ${0:.2f}'.format(subtotal))



RE: Limiting Decimals - FZMoto - Jul-01-2017

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.