如何获取float变量,并控制在不使用round()的情况下float的输出距离?例如。
w = float(1.678)
我要取x,然后用它做以下变量。
x = 1.67
y = 1.6
z = 1
如果我使用相应的圆方法:
x = round(w, 2) # With round I get 1.68
y = round(y, 1) # With round I get 1.7
z = round(z, 0) # With round I get 2.0
它会把数字变圆,到我没用的地步。我知道这是圆点,它工作正常。我怎样才能得到x,y,z变量所需要的信息,并且仍然能够在其他浮点数格式的方程中使用它们呢?
如果你只需要控制格式的精度
如果需要控制浮点运算的精度
--编辑--
没有“舍入”,因此截断数字
一个超级简单的解决方案是使用字符串
任何一个浮点库的解决方案都需要你回避一些舍入,并且使用10的floor/powers来挑选小数点,与上面的相比可能会有点毛茸茸的。
你可以:
测试:
相关问题 更多 >
编程相关推荐