Python 3.14+Numbers & mathfunction
float.from_number()
type conversion
Class method building a float from a number (int, float or an object with __float__). Unlike float(), it rejects strings. Python 3.14+.
Syntax
float.from_number(x)
The method exists to separate "convert a number" from "parse a string": float() does both, so a string that sneaks in from input() is silently accepted. from_number raises TypeError on str and bytes, yet it does not shield you from precision loss — a Decimal with a long mantissa is rounded, and an int too large for a float raises OverflowError.
Examples
print(float.from_number(7)) # → 7.0
print(float.from_number(-2.5)) # → -2.5
from decimal import Decimal
print(float.from_number(Decimal('1.25'))) # → 1.25
from fractions import Fraction
print(float.from_number(Fraction(1, 4))) # → 0.25
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy