If value is a float, the binary floating point value is losslessly converted to its exact decimal equivalent. This conversion can often require 53 or more digits of precision. For example, Decimal(float('1.1')) converts to Decimal('1.100000000000000088817841970012523233890533447265625').
prec
属性定义小数点后有多少个数字将对数字进行四舍五入。例如,如果期望2.00
,则其值应为3
。或者,如果要将数字四舍五入,使其没有小数位数,可以使用1
作为参数将字符串传递给十进制构造函数而不是浮点数:
Decimal('0.2')
给出预期的结果,Decimal(0.2)
没有这是因为:
https://docs.python.org/3/library/decimal.html#decimal.Decimal
从浮点数构造十进制数时,会得到浮点数的精确值,这可能与十进制数不完全匹配,因为浮点数就是这样工作的
如果要执行精确的十进制算术,请从字符串而不是浮点数构造十进制对象:
相关问题 更多 >
编程相关推荐