x="1,234.00"
y =x.replace(',','')
k = float(y)
print k
output=1234.0 but i need 1234.00 value please solve this problem
x="1,234.00"
y =x.replace(',','')
k = float(y)
print k
output=1234.0 but i need 1234.00 value please solve this problem
There is no difference in value between 1234.0 and 1234.00. You can't save more or less non-significant digits in a float.
However, you can print more or less (non-)significant digits. In older versions of python you can use the %
method (documentation). In Python 2.7 and up, use the string format method. An example:
f = float('156.2')
# in Python 2.3
no_decimals = "%.0f" % f
print no_decimals # "156" (no decimal numbers)
five_decimals = "%.5f" % f
print five_decimals # "156.20000" (5 decimal numbers)
# in Python 2.7
no_decimals = "{:.0f}".format(f)
print no_decimals # "156" (no decimal numbers)
five_decimals = "{:.5f}".format(f)
print five_decimals # "156.20000" (5 decimal numbers)
If you for some reason have no access to the code that prints the value, you can create your own class, inherit from float
and supply your own __str__
value. This could break some behaviour (it shouldn't, but it could), so be careful. Here is an example:
class my_float(float):
def __str__(self):
return "%.2f" % self
f = my_float(1234.0)
print f # "1234.00"
(I ran this on Python 2.7, I have 2.3 not installed; please consider upgrading Python to 2.7 or 3.5; 2.3 is seriously outdated.)