在Cheetah中格式化数字时如何添加千位分隔符?

时间:2018-02-12 03:00:49

标签: python-3.x cheetah

我试图将数字格式化为Cheetah3(Python 3.6)中的价格。虽然我使用标准格式表达式成功限制为2位小数,但逗号分隔符似乎不起作用(我收到错误)。

猎豹表达

#def format_price($price)
    #if $price < 0.0
        #set $price_str = '($%,.2f)' % (-1.0 * $price)
    #else
        #set $price_str = '$%,.2f' % $price
    #end if
    $price_str
#end def

错误

ValueError: unsupported format character ',' (0x2c) at ...

1 个答案:

答案 0 :(得分:1)

这不是猎豹问题。 ValueError直接来自Python:

$ python3.6
>>> '($%,.2f)' % -1.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: unsupported format character ',' (0x2c) at index 3

您可能想要.format()

相关问题