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
If you only care about 1 decimal place of precision (as in your examples), you can just do:
("%.1f" % i).replace(".0", "")
This will convert the number to a string with 1 decimal place and then remove it if it is a zero:
>>> ("%.1f" % 0).replace(".0", "")
'0'
>>> ("%.1f" % 0.0).replace(".0", "")
'0'
>>> ("%.1f" % 0.1).replace(".0", "")
'0.1'
>>> ("%.1f" % 1.0).replace(".0", "")
'1'
>>> ("%.1f" % 3000.0).replace(".0", "")
'3000'
>>> ("%.1f" % 1.0000001).replace(".0", "")
'1'