Formatting floats without trailing zeros

后端 未结 18 1501
再見小時候
再見小時候 2020-11-22 10:03

How can I format a float so that it doesn\'t contain trailing zeros? In other words, I want the resulting string to be as short as possible.

For example:

<         


        
18条回答
  •  囚心锁ツ
    2020-11-22 10:34

    Handling %f and you should put

    %.2f

    , where: .2f == .00 floats.

    Example:

    print "Price: %.2f" % prices[product]

    output:

    Price: 1.50

提交回复
热议问题