【问题标题】:Python: Thousands Separator - old Formatting SyntaxPython:千位分隔符 - 旧的格式化语法
【发布时间】:2018-08-16 08:38:23
【问题描述】:

使用新的格式化语法,Python 可以轻松打印千位分隔符:

print ("{0:,.2f} Euro".format(myVariable))

所以逗号可以解决这个问题。

如何在不使用语言环境的情况下使用旧语法来做到这一点?

print ("%.2f Euro" % myVariable)

【问题讨论】:

    标签: python string


    【解决方案1】:

    printf-style formatting 不支持千位​​分隔符。

    你需要构建一个带有千位分隔符的字符串:

    >>> amount = 12345
    >>> '%s Euro' % format(amount, ',.2f')
    '12,345.00 Euro'
    

    内置函数format 使用与str.format 相同的Format Specification Mini-Language

    如果您不能使用format,请参阅this question 了解替代方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 2014-03-10
      • 2018-09-05
      • 2022-07-31
      • 2013-05-16
      • 2016-10-07
      相关资源
      最近更新 更多