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
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
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
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