浮点乘法
Float multiplication
在python有没有人知道如何保存微不足道的0
,即
x=9999.0
y=.01
print x*y
输出是 99.99
但我想要 99.990
。这会发生吗?
我不确定格式化是否可以实现 this.In 简而言之,我需要保存 0
。
所以必须是generic.It也可以是
x=9999.5
y=.01
然后我们不需要格式化 there.Only 作为我们丢失的地方 0
。也不需要 5
数字 always.It 可以更少或 more.So我们需要以某种方式检测我们丢失的 0
然后附加它
您可以用string formatting指定小数点后的位数。在控制台中试试这个:
>>> print '%.3f' % (9999.0*.01)
99.990
在 2.7 版本中工作正常
x=9999.0
y=.01
print '%.3f' % (x*y)
了解 python 格式化 here
您可以使用十进制模块:
>>> from decimal import *
>>> getcontext().prec = 5
>>> Decimal(9999.0) * Decimal(0.01)
Decimal('99.990')
在python有没有人知道如何保存微不足道的0
,即
x=9999.0
y=.01
print x*y
输出是 99.99
但我想要 99.990
。这会发生吗?
我不确定格式化是否可以实现 this.In 简而言之,我需要保存 0
。
所以必须是generic.It也可以是
x=9999.5
y=.01
然后我们不需要格式化 there.Only 作为我们丢失的地方 0
。也不需要 5
数字 always.It 可以更少或 more.So我们需要以某种方式检测我们丢失的 0
然后附加它
您可以用string formatting指定小数点后的位数。在控制台中试试这个:
>>> print '%.3f' % (9999.0*.01)
99.990
在 2.7 版本中工作正常
x=9999.0
y=.01
print '%.3f' % (x*y)
了解 python 格式化 here
您可以使用十进制模块:
>>> from decimal import *
>>> getcontext().prec = 5
>>> Decimal(9999.0) * Decimal(0.01)
Decimal('99.990')