我的代码:
else:
revenue2 = revenue
stripLastLetter = revenue[len(revenue)-1:len(revenue)]
if stripLastLetter == "B":
result = revenue2[:-1]
revNum = float(result.replace(",", ""))
final = revNum * 1000000000
newnum = "{:0,.2f}".format(final)
#print(newnum)
elif stripLastLetter == "M":
result = revenue2[:-1]
revNum = float(result.replace(",", ""))
final = revNum * 1000000
newnum = "{:0,.2f}".format(final)
#print(newnum)
elif stripLastLetter == "K":
result = revenue2[:-1]
revNum = float(result.replace(",", ""))
final = revNum * 1000
newnum = "{:0,.2f}".format(final)
#print(newnum)
else:
result = revenue2
revNum = float(result.replace(",", ""))
final = revNum
newnum = "{:0,.2f}".format(final)
#print(newnum)
if float(newnum) > 50000000:
print(ticker.upper()+newnum)
else:
None
运行时,我的“newnum”变量看起来像这样:“56,100,000,000.00”,但仍然是字符串。
在我的上一个if语句中,我将newnum与int进行比较并绘制错误:“无法将字符串转换为float:'30,270,000,000.00'”
如何将newnum转换为float / int对象?
答案 0 :(得分:0)
这里的问题是你的字符串中有逗号:x / p>
newint = float(newnum.replace(',',''))