dropping trailing '.0' from floats

后端 未结 16 2161
傲寒
傲寒 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:21

    def floatstrip(x):
        if x == int(x):
            return str(int(x))
        else:
            return str(x)
    

    Be aware, though, that Python represents 0.1 as an imprecise float, on my system 0.10000000000000001 .

提交回复
热议问题