Python中相同计数的不同结果

我写了一个简单的计算,但是得到的值与样本不同。 Python明显如何四舍五入?

PriceListOld = 23 
PriceListNew = []

if PriceListOld <=20:
    PriceListNew = PriceListOld * 0.8
elif PriceListOld >20 and PriceListOld <=50:
    PriceListNew = PriceListOld * 0.6
elif PriceListOld >50:
    PriceListNew = PriceListOld * 0.4
print(PriceListNew)     

print(23 -(4*2.3))    #<-- This is the Sample. 
llf8282 回答:Python中相同计数的不同结果

您的演示代码不必要地复杂;您也可以写:

>>> print(23-4*2.3)    #<-- This is the Sample.
13.8
>>> print(23*0.6)
13.799999999999999
  

Python明显四舍五入如何发生?

由于0.6不能精确表示为二进制而发生。如果您只想得到13.8的结果,则可以摆脱

>>> print(23*6/10)
13.8
本文链接:https://www.f2er.com/3153273.html

大家都在问