If you're asking how to turn float_a = 12.200
into the string "12200", simply multiply it by 1000
before converting it into a string, for example:
print(str(float_a * 1000))
However, if the number contains more than 4 decimal places, you'll still have decimals.
However, since you're talking about it "always removing the 0's", I suspect you may have missed a .
in your expected output. In this case, to display a float
with a fixed number of decimals, just use Python's built-in string formatting:
float_a = 12.200
expected_result = '%.2f' % float_a
print expected_result
> 12.20
If this doesn't make sense, please clarify your question a bit and I'll edit! :)
Edit
Upon your further clarification of the question, it seems that you want to define a float
with trailing zeros and have them preserved and the float
converted to a string with the leading zeros preserved. I'm afraid this is impossible if your input type is float
, since float
won't preserve those trailing zeros to begin with:
In [1]: float_a = 3.1400
In [2]: float_a
Out[2]: 3.14
This is a "limitation" of the data type and there's nothing you can do about it. You want a fixed-point type or something to handle "decimal numbers", not a float.