我在 Python 中使用 Decimal 库,并使用
format(value, 'f')
,其中 value 是 Decimal
。我得到 10.00000
形式的数字,它反射(reflect)了小数点的精度。我知道float
支持is_integer
,但是似乎缺少类似的小数API。我想知道是否有办法解决这个问题。
最佳答案
您可以使用模运算来检查是否存在非整数余数:
>>> from decimal import Decimal
>>> Decimal('3.14') % 1 == 0
False
>>> Decimal('3') % 1 == 0
True
>>> Decimal('3.0') % 1 == 0
True
关于python Decimal - 检查是否为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19965018/