user3434967
user3434967

Reputation: 11

'float' object is not subscriptable

I'm trying to make a cash register, but when I do the tax, I want to have it with two decimal places, instead of something like $3.006743

I tried to do this:

elif item == 'done':
    os.system('cls')
    tprice = sprice * 1.13
    hst = tprice - sprice
    if tprice >= 1:
        tprice = tprice[0:5]
    elif tprice >= 10:
        tprice = tprice[0:6]
    elif tprice >= 100:
        tprice = tprice[0:7]
    if hst >= 1:
        hst = hst[0:5]
    elif hst >= 10:
        hst = hst[0:6]
    elif hst >= 100:
        hst = hst[0:7]
    print(receipt)

But I get the error. Any help?

Upvotes: 1

Views: 6881

Answers (2)

Aaron
Aaron

Reputation: 2351

If you are using this for currencies I would also recommend looking at python limiting floats to two decimal points, because rounding floats will cause your calculations to be incorrect. Many people store cents (in an integer) and divide by 100, or use python's Decimal type.

Upvotes: 1

Hyperboreus
Hyperboreus

Reputation: 32429

You can use string formatting:

>>> '{:.2f}'.format(3.123456) 
'3.12'
>>> '$ {:.2f}'.format(12.999)
'$ 13.00'
>>> '{:.2f}'.format(.029)
'0.03'

Upvotes: 2

Related Questions