Python Glossary Русский

all Python 3Numbers & mathfunction

math.modf

representation

Return the (fractional, integer) parts as a pair of floats.

Syntax

math.modf(...)

The order trips people up: the fractional part comes first, the integer part second. Both are floats and both carry the sign of the input, so modf(-3.5) is (-0.5, -3.0) — unlike divmod(-3.5, 1), which floors and yields (-4.0, 0.5). If you need the integer part as an int, use int(x) or math.trunc(x); modf never gives you one.

Examples

import math
print(math.modf(3.5))   # → (0.5, 3.0)
print(math.modf(-3.5))   # → (-0.5, -3.0)
print(math.modf(5.0))   # → (0.0, 5.0)
print(math.modf(2.7))   # → (0.7000000000000002, 2.0)
print(round(math.modf(19.99)[0] * 100))   # → 99

See also

Open in the full glossarydocs.python.orgReport an inaccuracy