相关疑难解决方法(0)

为什么"%.10f"%Decimal(u)会发出带有字面冒号的字符串?

格式化要打印的数字时,正在使用点后面的冒号格式化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)

python aix string-formatting

40
推荐指数
1
解决办法
1549
查看次数

Python通过pyodbc在Access中以十进制数字插入冒号

我遇到了和这个家伙一样的问题,也可能是这个家伙,但我正在分享一些代码并回答问题!

我在批处理作业中有一些代码,它通过pyodbc从Microsoft Access数据库中读取字段,并准备输出以供显示.

这是一个片段.请注意断言.

def format_currency(amount):
    if amount is None:
        return ""
    else:
        result = "$%.2f" % amount
        assert ":" not in result, (
            "That's weird. The value %r of class %s is represented as %s" %
             (amount, amount.__class__, result))
        return result
Run Code Online (Sandbox Code Playgroud)

当我运行它时,它成功处理100,000行然后失败:

AssertionError: That's weird. The value Decimal('54871.0000') of class <class
'decimal.Decimal'> is represented as $54870.:0
Run Code Online (Sandbox Code Playgroud)

注意异常的结肠.它很少发生 - 大约有300,000条记录中的一次.

当我试图隔离它时,它当然有效.

from decimal import Decimal
print "$%.2f" % Decimal('54871.0000')
Run Code Online (Sandbox Code Playgroud)

$ 54871.00

Access中字段的类型是:

  • 数据类型:货币
  • 小数位:2
  • 输入掩码:
  • 默认值: …

python ms-access pyodbc

5
推荐指数
1
解决办法
968
查看次数

标签 统计

python ×2

aix ×1

ms-access ×1

pyodbc ×1

string-formatting ×1