以下类似的帖子并不能回答我的问题。 将字符串转换为带有小数的整数
考虑以下Python代码。
>>> import decimal
>>> s = '23.456'
>>> d = decimal.Decimal(s)
>>> d
Decimal('23.456') # How do I represent this as simply 23.456?
>>> d - 1
22 # How do I obtain the output to be 22.456?
如何将字符串转换为十进制数,以便能够执行算术运算并获得正确精度的输出?
x = float(23.456) - 1
出了什么问题? - RanRagDecimal
和float
之间的区别:http://docs.python.org/library/decimal.html - LevonDecimal
在表示十进制(基数为10)数字时更加“精确”;而float
在表示二进制(基数为2)数字时更加精确。 - Andrew Jaffe