How to use Python string formatting to convert an integer representing cents to a float representing dollars?

后端 未结 3 1385
谎友^
谎友^ 2020-12-10 17:31

I have an integer representing a price in cents. Using Python format strings, how can I convert this value into dollars with two decimal places? Examples:

         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-10 18:14

    Using str.format:

    for i in (1234,5,999):
        print('{:.2f}'.format(i/100.))
    

    yields

    12.34
    0.05
    9.99
    

    In Python2.6 use '{0:.2f}' instead of '{:.2f}'.

提交回复
热议问题