dropping trailing '.0' from floats

后端 未结 16 2221
傲寒
傲寒 2020-12-05 03:54

I\'m looking for a way to convert numbers to string format, dropping any redundant \'.0\'

The input data is a mix of floats and strings. Desired output:

0

16条回答
  •  执念已碎
    2020-12-05 04:18

    So much ugliness out there…

    My personal favorite is to convert floats that don't require to be a float (= when they actually are integers) to int, thus removing the, now useless, trailing 0

    (int(i) if i.is_integer() else i for i in lst)

    Then you can print them normally.

提交回复
热议问题