Can't multiply some items by 1000 without kicking out the decimal places

Question:

After eliminating a letter B from some items, I can’t multiply them by 1000 without kicking out the decimal places.

If I try like int(float(item[:-1])) * 1000, the decimal places are removed in the first place, and I get wrong answers.

However, I get this error: ValueError: invalid literal for int() with base 10: '92.96' when I try something like below.

items = ['92.96B','85.4B','33B']

for item in items:
    converted_item = int(item[:-1]) * 1000
    print(converted_item)

Expected output:

92960
85400
33000
Asked By: MITHU

||

Answers:

Instead of int() convert them to float(), then multiply by 1000 and finally convert them to int()

You cannot directly use int() on a string representing a floating number.

items = ['92.96B','85.4B','33B']

for item in items:
    converted_item = int(float(item[:-1]) * 1000)
    print(converted_item)


#output
92960
85400
33000
Answered By: God Is One
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.