I have a string that I have to convert into int or float depending or the case :
What I have => What I want
"548189848.54" => 548189848.54
"548189848.50" => 548189848.5
"548189848.00" => 548189848
Is it possible to do it ?
Thanks,
Steve
解决方案
Maybe you could convert to float and then use round:
inputs = [ "548189848.54", "548189848.50", "548189848.00" ]
for i in inputs:
f = float(i)
if round(f) == f:
print int(f)
else:
print f
output:
548189848.54
548189848.5
548189848
You could also do the same thing using a list comprehension, like:
print [int(float(i)) if round(float(i)) == float(i) else float(i) for i in inputs]
output:
[548189848.54, 548189848.5, 548189848]