0

Why isn't working, i got the error : TypeError: 'float' object cannot be interpreted as an integer

My code

qtyprice = 0.5221932114882507
lot_size = 0.1

quantity = float(round(qtyprice, lot_size))
print (quantity)

My expected result should be 0.5

When i try this way, the result is 0

qtyprice = 0.5221932114882507
lot_size = 0.1
qtyprice = int(qtyprice)
lot_size = int(lot_size)
quantity = float(round(qtyprice, lot_size))
print (quantity)
3
  • 2
    Change lot_size to 1. The second parameter in round is how many decimal digits to have, it should be a positive int. Commented Aug 3, 2022 at 5:53
  • round(number, digits) - number: Required. The number to be rounded - digits: Optional. The number of decimals to use when rounding the number. Default is 0 Commented Aug 3, 2022 at 5:59
  • sometimes the lot size is 0.01, my output would be 0.52, so how to do in this case? Commented Aug 3, 2022 at 6:30

1 Answer 1

1

the second parameter of round is the number of digits after the decimal point you want to "keep" after rounding your number, this number should be an int and in your case should be 1

this should do the trick :

qtyprice = 0.5221932114882507
lot_size = 1 #what i changed 

quantity = float(round(qtyprice, lot_size))
print (quantity)

output:

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

2 Comments

sometimes the lot size is 0.01, my output would be 0.52, so how to do in this case?
I would change lot_size to be 2 (since you want to keep 2 digits after the decimal point)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.