Python 我怎样才能在非科学记数法中得到一个大数字?
我刚试过Python 我怎样才能在非科学记数法中得到一个大数字?,python,formatting,number-formatting,Python,Formatting,Number Formatting,我刚试过 >>> 2.17 * 10**27 2.17e+27 >>> str(2.17 * 10**27) '2.17e+27' >>> "%i" % 2.17 * 10**27 Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: cannot fit 'long' into an index-
>>> 2.17 * 10**27
2.17e+27
>>> str(2.17 * 10**27)
'2.17e+27'
>>> "%i" % 2.17 * 10**27
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: cannot fit 'long' into an index-sized integer
>>> "%f" % 2.17 * 10**27
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: cannot fit 'long' into an index-sized integer
>>> "%l" % 2.17 * 10**27
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: incomplete format
我怎么能打印这么大的数字?(我不在乎它是Python2.7+解决方案还是Python3.X解决方案)您的运算符优先级搞错了。您正在格式化
2.17
,然后将其乘以一个长整数:
>>> r = "%f" % 2.17
>>> r
'2.170000'
>>> r * 10 ** 27
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: cannot fit 'long' into an index-sized integer
这是为字符串格式重载模运算符的缺点之一;和它所使用的(并且可以与一起使用)的更新版本巧妙地避开了该问题。对于这种情况,我会使用format()
:
>>> format(2.17 * 10**27, 'f')
'2169999999999999971109634048.000000'
您的运算符优先级搞错了。您正在格式化
2.17
,然后将其乘以一个长整数:
>>> r = "%f" % 2.17
>>> r
'2.170000'
>>> r * 10 ** 27
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: cannot fit 'long' into an index-sized integer
这是为字符串格式重载模运算符的缺点之一;和它所使用的(并且可以与一起使用)的更新版本巧妙地避开了该问题。对于这种情况,我会使用format()
:
>>> format(2.17 * 10**27, 'f')
'2169999999999999971109634048.000000'
谢谢你。现在我读了你的答案,它是讣告。谢谢你把这么多细节放在里面!谢谢你。现在我读了你的答案,它是讣告。谢谢你把这么多细节放在里面!