为什么“%.10f”%Decimal(u)发出一个带有文字冒号的string?

格式化要打印的号码时,紧跟在点后面的十二位数字正在用冒号格式化。 为什么发生这种情况? 这是AIX系统上的Python 2.7。

$ uname -a ; /opt/bin/python2.7 AIX myserver 1 6 00F6A5CC4C00 Python 2.7.12 (default, Sep 29 2016, 12:02:17) [C] on aix5 Type "help", "copyright", "credits" or "license" for more information. >>> '{0:.10f}'.format(123456789012) '123456789011.:000000000' >>> from decimal import Decimal >>> u=123456789012 >>> print "%.10f" % Decimal(u) 123456789011.:000000000 

更多信息:

这不是每12位数字:

 >>> for x in range(123456789010,123456789020): ... print '{0:.10f}'.format(x) ... 12345678900:.0000000000 123456789010.:000000000 123456789011.:000000000 123456789013.0000000000 123456789013.:000000000 123456789015.0000000000 123456789016.0000000000 123456789017.0000000000 123456789017.:000000000 123456789019.0000000000 

这不会发生任何其他长度的数字。 另外,我尝试了bash和perl的printf,这两个都没有发生。

这里发生了什么?

根据要求,这是一个截图video 。

更多请求的信息:

 >>> import locale >>> locale.getdefaultlocale() ('en_US', 'ISO8859-1') 

user2357112 pastebin代码的结果:

 >>> import ctypes >>> f=ctypes.pythonapi.PyOS_double_to_string >>> f.argtypes=ctypes.c_double,ctypes.c_char,ctypes.c_int,ctypes.c_int,ctypes.POINTER(ctypes.c_int)) >>> f.restype=ctypes.c_char_p >>> print f(123456789012.0, 'f', 10, 0, None) 123456789011.:000000000 

Antti_Happa的pastebin正确地打印所有数字。

使用格式r给出:

 print 'e: {0:.10e}\nf: {0:.10f}\ng: {0:.10g}\nr: {0:0r}'.format(x) ValueError: Unknown format code 'r' for object of type 'int' 

使用e,f和g格式提供了以下内容:

 for x in range(123456789000,123456789019): print 'e: {0:.10e}\nf: {0:.10f}\ng: {0:.10g}'.format(x) e: 1.2345678900e+11 f: 123456789000.0000000000 g: 1.23456789e+11 e: 1.2345678900e+11 f: 123456789000.:000000000 g: 1.23456789e+11 e: 1.2345678900e+11 f: 123456789001.:000000000 g: 1.23456789e+11 e: 1.2345678900e+11 f: 123456789003.0000000000 g: 1.23456789e+11 

我无法安装或更新此服务器上的任何内容。 我可以要求一个更新的版本,但是改变这种性质的请求需要相当长的时间。 另外,其他程序依赖于这个安装,并且需要大量的testing。

我被告知,只有IBM提供的软件包将被安装,并且IBM提供的最新的Python 2.7包是2.7.12。

我通过这样做来“解决”了问题

 othervar = '{0:.10f}'.format(somevar).replace(':', '0') 

这是非常不安全的,我知道,但是… 耸耸肩

哎呀! 我只是注意到一个123456789012错误… 123456789012被格式化为一个less: 123456789011.:0000000000 : 123456789011.:0000000000 …这是一个奇怪的错误。

虽然不是“答案”,但我可以在AIX上运行稍微更新版本的结果。

对不起,我无法复制您的问题。

 [lholtscl@ibm3 ~]$ python Python 2.7.13 (default, Sep 7 2017, 21:08:50) [C] on aix7 Type "help", "copyright", "credits" or "license" for more information. >>> print "%.10f" % 123456789012 123456789012.0000000000 >>> '{0:.10f}'.format(123456789012) '123456789012.0000000000'