Complete guide to Python's float function covering number conversion, string parsing, and practical examples of floating-point operations.
Last modified April 11, 2025
This comprehensive guide explores Python’s float function, which converts numbers and strings to floating-point values. We’ll cover conversion rules, string parsing, special values, and practical examples.
The float function creates a floating-point number from a number or string. It implements Python’s floating-point type which follows IEEE 754 standard for double precision (64-bit) numbers.
Key characteristics: converts integers, strings with decimal numbers, scientific notation. Returns special values like inf and nan for certain inputs. Default argument returns 0.0.
Here’s simple usage showing how float converts different numeric types and string representations to floating-point numbers.
basic_float.py
print(float(42)) # 42.0 print(float(-10)) # -10.0
print(float(“3.14”)) # 3.14 print(float("-1.5")) # -1.5
print(float(“2.5e2”)) # 250.0
This example shows float converting integers and strings. Integer inputs gain a decimal point. String parsing handles signs and decimal points.
Scientific notation (e/E) is supported in strings. The exponent indicates power of 10 multiplication (e2 means ×10²).
Python’s float supports special values like infinity and NaN (Not a Number). This example demonstrates their creation and checking.
special_values.py
pos_inf = float(“inf”) neg_inf = float("-inf") print(pos_inf, neg_inf) # inf -inf
nan = float(“nan”) print(nan) # nan
import math print(math.isinf(pos_inf)) # True print(math.isnan(nan)) # True
Infinity values can be created with string “inf” or “-inf”. NaN represents undefined numerical results like 0/0. These are IEEE 754 standard values.
The math module provides functions to check for these special values, as direct equality comparisons with NaN are unreliable.
float has specific string parsing rules. This example shows valid and invalid formats, including locale-dependent decimal points.
string_parsing.py
print(float(“3.14”)) # 3.14 print(float(" 3.14 “)) # 3.14 (whitespace ignored) print(float(“3.14e-2”)) # 0.0314
try: float(“3.14.15”) # Multiple decimal points except ValueError as e: print(f"Error: {e}”)
try: float(“3,14”) # Comma as decimal (locale-dependent) except ValueError as e: print(f"Error: {e}")
import locale locale.setlocale(locale.LC_NUMERIC, ‘de_DE’) print(float(“3,14”)) # Still fails - float() doesn’t use locale
float only accepts period as decimal point, regardless of system locale. For locale-specific parsing, additional processing is needed.
Whitespace around numbers is ignored. The function raises ValueError for malformed strings or unsupported formats.
This example demonstrates proper error handling when converting uncertain inputs, and shows the default behavior of float.
error_handling.py
print(float()) # 0.0
def safe_float(value, default=0.0): try: return float(value) except (ValueError, TypeError): return default
print(safe_float(“3.14”)) # 3.14 print(safe_float(“abc”)) # 0.0 print(safe_float(None)) # 0.0 print(safe_float("", 99.9)) # 99.9 (custom default)
The safe_float function demonstrates robust conversion that won’t raise exceptions. It returns a default value for unconvertible inputs.
Without arguments, float() returns 0.0. This is useful for initializing variables or providing default numeric values.
Floating-point numbers have inherent precision limitations. This example demonstrates common representation issues and how to handle them.
precision.py
num = float(“0.1”) + float(“0.2”) print(num) # 0.30000000000000004 print(num == 0.3) # False
from decimal import Decimal num = Decimal(“0.1”) + Decimal(“0.2”) print(float(num)) # 0.3
big = float(“12345678901234567890”) print(big) # 1.2345678901234567e+19
tiny = float(“1e-324”) print(tiny) # 0.0
Floating-point arithmetic can produce unexpected results due to binary representation. The decimal module provides exact decimal arithmetic.
Very large or small numbers may lose precision or underflow to zero. These are limitations of 64-bit floating-point representation.
Use for conversion: Convert strings/numbers when float type needed
Handle errors: Catch ValueError for string conversion attempts
Consider alternatives: Use decimal for financial calculations
Watch precision: Be aware of representation limitations
Document assumptions: Note when float precision is sufficient
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.