我写了一个函数来舍入值,但是当我向下舍入65.80时我遇到了麻烦。它给了我65.7,而不是65.8。
这是我的代码:
def quantize_down(x):
return float(Decimal(x).quantize(Decimal('.1'), rounding='ROUND_DOWN'))
print("quantize_down(65.8)")
print("Result", quantize_down(65.8))
print('-------------')
print("quantize_down(65.80):")
print("Result:", quantize_down(65.80))
print('-------------')
print("quantize_down(65.81)")
print("Result", quantize_down(65.81))
print('-------------')
print("quantize_down(65.87)")
print("Result", quantize_floor(65.87))
print('-------------')
然后我明白了:
quantize_down(65.8)
Result 65.7
-------------
quantize_down(65.80):
Result: 65.7
-------------
quantize_down(65.81)
Result 65.8
-------------
quantize_down(65.87)
Result 65.8
-------------
但我想要这个:
quantize_down(65.8)
Result 65.8
-------------
quantize_down(65.80):
Result: 65.8
-------------
quantize_down(65.81)
Result 65.8
-------------
quantize_down(65.87)
Result 65.8
-------------
我能做些什么呢?