-1

Im trying to calculate a bill in python for hw. Cant figure out how to round the numbers to two decimal places. Here's what I have so far. Every time I try the round function, it doesnt work and gives me an error message. Help?!

ss_cost = 3.95 * 2
hb_cost = 8.95 * 2
ds_cost = 2.50 * 2
subtotal = (ss_cost) + (hb_cost) + (ds_cost)
tax = (round(subtotal * 0.0475), %.2)
print (tax)`
elvenenby
  • 1
  • 1
  • 2
  • [`round`](https://docs.python.org/3/library/functions.html?highlight=round#round), `(round(subtotal * 0.0475), %.2)` changed to `round(subtotal * 0.0475, 2)`. – pppig Jan 16 '22 at 04:51
  • When it comes to amounts, you shouldn't use `round`, you should use [`decimal`](https://docs.python.org/3/library/decimal.html?highlight=round#module-decimal), – pppig Jan 16 '22 at 04:52
  • When you google your question title, the tagged duplicate comes up *as the first result*. Next time, do some **basic research** before you ask. – MattDMo Jan 16 '22 at 05:22

2 Answers2

0

You missplaced the %.2:

tax = (round(subtotal * 0.0475, 2))

And you don't need the %.

Leonardo Lima
  • 373
  • 2
  • 10
0

round(number[, ndigits])

Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.

For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2). Any integer value is valid for ndigits (positive, zero, or negative). The return value is an integer if ndigits is omitted or None. Otherwise, the return value has the same type as number.

When it comes to amounts, you shouldn't use round, it's better to use decimal.

from decimal import Decimal


ss_cost = 3.95 * 2
hb_cost = 8.95 * 2
ds_cost = 2.50 * 2
subtotal = (ss_cost) + (hb_cost) + (ds_cost)
tax = Decimal(subtotal * 0.0475).quantize(Decimal("0.00"))
print (tax)

# 1.46
pppig
  • 1,215
  • 1
  • 6
  • 12