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

6
  • 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. Commented Sep 9, 2021 at 1:06
  • Does this answer your question? Limiting floats to two decimal points Commented Sep 9, 2021 at 1:07
  • "%.2f" , this is what my professor uses, but I can't figure out how to add this into my code correctly. Commented Sep 9, 2021 at 1:13
  • print("Sales tax %.2f" % tax) Commented Sep 9, 2021 at 2: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. Commented Sep 14, 2021 at 15:58

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

unfortunately, this isn't what my professor taught me so I cant use this, but thank you.
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.

3 Comments

unfortunately, this isn't what my professor taught me so I cant use this, but thank you.
but yes I am running into the problem where the outputs arent spaced correctly
how would you make it so that the input is also 2 decimals?
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)''''

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.