Converting Float to Dollars and Cents

后端 未结 6 1088
北恋
北恋 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 10:49

    you said that:

    `mony = float(1234.5)
    print(money)      #output is 1234.5
    '${:,.2f}'.format(money)
    print(money)
    

    did not work.... Have you coded exactly that way? This should work (see the little difference):

    money = float(1234.5)      #next you used format without printing, nor affecting value of "money"
    amountAsFormattedString = '${:,.2f}'.format(money)
    print( amountAsFormattedString )
    

提交回复
热议问题