[英]Python Floating Point Rounding
Trying to figure out the correct rounding technique: 试图找出正确的舍入技术:
v = float(input("Enter V: "))
print ("V is %.3f" % str(round(v,3)))
Where am I going wrong? 我要去哪里错了?
str(round(v,3)
is the problem here, just remove the str
这是问题所在,只需删除str
you're changing into a string, which is wrong 您正在更改为字符串,这是错误的
Using str
turns the float into a string. 使用str
会将float转换为字符串。 Remove the str
: 删除str
:
print ("V is %.3f" % round(v, 3))
UPDATE 更新
As Barnie suggested in a comment above: there's no need for rounding since you're printing using %.3f
so you do even better: 正如Barnie在上面的评论中所建议的那样:由于您使用%.3f
打印,因此不需要四舍五入,因此您甚至可以做得更好:
print ("V is %.3f" % v)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.