如何将货币价值与" $"每个号码旁边,不只是在边缘?

时间:2015-11-12 06:46:44

标签: python formatting python-3.4

我正试图将一些货币价值对齐,如下所示:

John Smith         $3,500
Jane Doe             $200
Jack Johnson         $700

但我得到了这个:

John Smith         $3500
Jane Doe           $ 200
Jack Johnson       $ 700.

我的输出代码如下所示:

for (name, amount) in cursor:
    print("{}".format(name)+ "\t\t\t" + "${:>5,.0f}".format( amount))

我还想为数千个数字添加一个逗号分隔符。我知道我之前已经看过这种格式,但我很难再找到它。

2 个答案:

答案 0 :(得分:0)

试试这个,删除数字字符串两边的空格。

for (name, amount) in cursor: 
    print("{}".format(name)+ "\t\t\t" + "{:>5}".format("$"+ "{:.0f}".format(amount).strip()))

答案 1 :(得分:-1)

试试这个

for (name,amount) in cursor:
     print("{}".format(name) + "\t\t\t"  +  "${}".format(int(amount)))