繁体   English   中英

舍入浮点数类型错误:'float' object 不能解释为 integer

[英]Rounding float numbers TypeError: 'float' object cannot be interpreted as an integer

为什么不起作用,我收到错误:TypeError: 'float' object 不能被解释为 integer

我的代码

qtyprice = 0.5221932114882507
lot_size = 0.1

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

我的预期结果应该是 0.5

当我尝试这种方式时,结果为 0

qtyprice = 0.5221932114882507
lot_size = 0.1
qtyprice = int(qtyprice)
lot_size = int(lot_size)
quantity = float(round(qtyprice, lot_size))
print (quantity)

round 的第二个参数是round后要“保留”的小数点后的位数,这个数字应该是一个 int,在你的情况下应该是 1

这应该可以解决问题:

qtyprice = 0.5221932114882507
lot_size = 1 #what i changed 

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

output:

0.5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM