0

Write a program that asks the user for the price of an item, and then prints out a bill for the item. The bill includes the item price, the sales tax on the item, and the total amount paid by the customer. The sales tax is 5% of the item price. The total bill is the price of the item plus the sales tax.

The bill, if the item cost $99, would look like this:

Price $ 99.00 Sales tax 4.95 Total $ 103.95

This is what I have so far:

price = float(input("Price $"))
tax = .05
salestax = (price*tax)
total = ((price*tax)+ price)
print("Sales tax  ", salestax)
print("Total $", total)

Can someone help me with making the printed values have two decimal places?

Right now this is my output:

Price $99
Sales tax   4.95
Total $ 103.95

Clearly, the spacing is wrong, and not all of the dollar amounts have 2 decimal places

jarrodparkes
  • 2,378
  • 2
  • 18
  • 26
Matt Triano
  • 7
  • 1
  • 5
  • Convert it to string, split by `.`, and see if the second element exists. If so, count the digits and fill the remaining with `0`. If not, add `.00` to the end. – no ai please Sep 09 '21 at 01:06
  • Does this answer your question? [Limiting floats to two decimal points](https://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points) – Chris Sep 09 '21 at 01:07
  • "%.2f" , this is what my professor uses, but I can't figure out how to add this into my code correctly. – Matt Triano Sep 09 '21 at 01:13
  • `print("Sales tax %.2f" % tax)` – John Gordon Sep 09 '21 at 02:24
  • If your professor expects a certain formatting method, then make that known in your question statement. It sounds as-if your professor expects a printf-style output, and your question is mainly focused on how to print a decimal value with a scale (the number of digits right of the decimal) of two. – jarrodparkes Sep 14 '21 at 15:58
  • It would also be useful to know the version of Python being used or expected for your answer. Python 3 is the standard nowadays. – jarrodparkes Sep 14 '21 at 16:03

3 Answers3

2

You can use print(f'{total:.2f}) to print the first 2 numbers after the float point. Or, if you want, you can change the quantity of numbers changing the ".2f"in the sentence.

Or, you can use the round() function. Try this: round(total, 2) The 2 number represents how many number will appear after the dot.

Piai
  • 61
  • 1
1

I'm assuming you're having the issue when a float is entered as price ($99.99).

You could use str(round(total, 2)) to round your numbers to the 2nd decimal place.

price = float(input("Price $"))
tax = .05
salestax = (price*tax)
total = ((price*tax)+ price)
print("Sales tax  $" + str(round(salestax, 2)))
print("Total $" + str(round(total, 2)))

outputs to:

Price $45
Sales tax  $2.25
Total $47.25

You may run into rounding errors where the sales tax will only list 1 decimal place like $5.0. But should work for most prices.

spanky
  • 25
  • 7
0
num = 3.1356548655458
rNum = round(num,2)
print(rNum)

result will be 3.14 ''''round (the Number,How much digits you want after decimal)''''