为什么“%.10f”可以%Decimal(u)发出带有文字冒号的字符串? [英] Why can "%.10f" % Decimal(u) emit a string with a literal colon?

查看:238
本文介绍了为什么“%.10f”可以%Decimal(u)发出带有文字冒号的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

格式化要打印的数字时,点后紧接着用冒号格式化12位数字。为什么会这样呢?这是AIX系统上的Python 2.7。

When formatting a number to be printed, 12 digit numbers are being formatted with a colon immediately after the dot. Why is this happening? This is Python 2.7 on an AIX system.

$ 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,但是这两个都没有发生。

This is not happening with any other length numbers. Also, I tried bash's and perl's printf and this is not happening with either of them.

这里发生了什么?

根据要求,这是屏幕截图视频

更多请求的信息:

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

结果的user2357112粘贴代码:

Result of user2357112 pastebin code:

>>> 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正确打印了所有数字。

Antti_Happa's pastebin printed all numbers correctly.

使用格式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格式提供以下内容:

Using e, f and g formats provided the following:

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

我无权在此服务器上安装或更新任何内容。我可以请求更新的版本,但是这种性质的更改请求需要花费大量时间。另外,其他程序也依赖于此安装,并且需要进行大量测试。

I have no access to install or update anything on this server. I can request an updated version, but change requests of this nature take a fair amount of time. Also, other programs depend on this installation and a lot of testing would be required.

我被告知仅将安装IBM提供的软件包,而最新的python 2.7 IBM提供的软件包是2.7.12。

I have been informed that only IBM provided packages will be installed and that the latest python 2.7 package provided by IBM is 2.7.12.

我已经通过这样做来解决了这个问题

I have "fixed" the problem by doing

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

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

which is extremely unsafe, I know, but ... shrug

啊!我只是注意到一个不合一的错误... 123456789012 被格式化为以下格式: 123456789011.:0000000000 ...这是一个奇怪的错误。

Argh! I just noticed an off-by-one error ... 123456789012 is being formatted as one less: 123456789011.:0000000000 ... this is a weird bug.

推荐答案

虽然不是答案,但我可以在

Although not an "answer", I can give you my results on a slightly newer version running on AIX.

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

Sorry that I'm unable to replicate your problem.

[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'

这篇关于为什么“%.10f”可以%Decimal(u)发出带有文字冒号的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆