Next / Previous / Contents / TCC Help System / NM Tech homepage

20.15. float(): Convert to float type

Converts a value to type float. The argument must be a number, or a string containing a numeric value in string form (possibly surrounded by whitespace). If the argument is not a valid number, this function will raise a ValueError exception. If no argument is given, it will return 0.0.

>>> float()
0.0
>>> float(17)
17.0
>>> float(' 3.1415  ')
3.1415000000000002
>>> print float('6.0221418e23')
6.0221418e+23
>>> float('142x')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): 142x