Converting Float to Dollars and Cents

后端 未结 6 1085
北恋
北恋 2020-12-02 10:39

First of all, I have tried this post (among others): Currency formatting in Python. It has no affect on my variable. My best guess is that it is because I am using Python

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 11:04

    In Python 3.x and 2.7, you can simply do this:

    >>> '${:,.2f}'.format(1234.5)
    '$1,234.50'
    

    The :, adds a comma as a thousands separator, and the .2f limits the string to two decimal places (or adds enough zeroes to get to 2 decimal places, as the case may be) at the end.

提交回复
热议问题