格式化要打印的数字时,正在使用点后面的冒号格式化12位数字.为什么会这样?这是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
Run Code Online (Sandbox Code Playgroud)
更多的信息:
它不是每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
Run Code Online (Sandbox Code Playgroud)
任何其他长度数字都不会发生这种情况.另外,我尝试了bash和perl的printf,而且这两种情况都没有发生.
这里发生了什么?
根据要求,这是一个截屏视频.
更多要求的信息:
>>> import locale
>>> locale.getdefaultlocale()
('en_US', …Run Code Online (Sandbox Code Playgroud) 我有一个从文本文件中读入的浮点数列表.经过一些数据处理后,我使用以下命令将列表写入文件:
for val in flist:
sa = '{0}'.format(val)
fout.write(sa)
Run Code Online (Sandbox Code Playgroud)
对于特定的输入文件,输出文件在字符串中将包含":".我已经运行调试并在失败时停止了脚本.值应为58710000.0
[Dbg]>>> print val[464]
5870:000.0
[Dbg]>>> fa = val[464]
[Dbg]>>> print fa
5870:000.0
[Dbg]>>>
[Dbg]>>> fa = fa + 1
[Dbg]>>> print fa
58710001.0
[Dbg]>>> fa = fa - 1
[Dbg]>>> print fa
5870:000.0
Run Code Online (Sandbox Code Playgroud)
这仅适用于某些文件和浮点数
有什么建议?