How to Get the Decimal Part of a Number in Python

To get the decimal part of a float in Python, call math.modf(x). It returns the fractional and integer parts together, both signed, so a -3.75 refund gives -0.75.

Below are four tested ways to get the decimal part of a number in Python on Python 3.12.5, plus where x % 1 gets negatives wrong.

The Sample Receipt Totals We’ll Use in Python

You’ll reuse five line totals from a Denver bike shop receipt, saved as prices.py:

# Denver bike shop receipt lines (USD)
PRICES = [19.99, 249.50, -3.75, 7.00, 1234.56]

The -3.75 refund is the tricky row. To drop the decimals instead, see how to remove decimal numbers in Python.

Method 1: Split a Float into Integer and Decimal Parts with math.modf() (Recommended)

Use math.modf() to get both parts of a float in one call, here on the same receipt values:

import math
from prices import PRICES

for price in PRICES:
    frac, whole = math.modf(price)
    print(f"{price:>8}  integer {whole:>7}  decimal {round(frac, 2)}")

print(math.modf(19.99))

Output:

   19.99  integer    19.0  decimal 0.99
   249.5  integer   249.0  decimal 0.5
   -3.75  integer    -3.0  decimal -0.75
     7.0  integer     7.0  decimal 0.0
 1234.56  integer  1234.0  decimal 0.56
(0.9899999999999984, 19.0)
Python get decimal part: VS Code running math.modf on five receipt totals, showing integer and decimal parts
Each total split in two; the refund keeps its minus sign.

How does this code work?

math.modf(19.99) returns (0.9899999999999984, 19.0), fraction first. That tail is float noise, so round it as in rounding numbers to 2 decimal places in Python.

No import? price - int(price) gives the same -0.75, since int() cuts toward zero (converting a float to an int in Python).

Method 2: Get the Fractional Part with the Modulo Operator (% 1)

x % 1 is the shortest option. Same receipt values, refund included:

from prices import PRICES

for price in PRICES:
    print(f"{price:>8}  % 1 -> {price % 1:.2f}")

print(divmod(-3.75, 1))

Output:

   19.99  % 1 -> 0.99
   249.5  % 1 -> 0.50
   -3.75  % 1 -> 0.25
     7.0  % 1 -> 0.00
 1234.56  % 1 -> 0.56
(-4.0, 0.25)
VS Code output of Python x % 1 on receipt totals, where -3.75 returns 0.25 instead of -0.75
The -3.75 refund comes back as 0.25.

How does this code work?

Important: Python’s % floors, so -3.75 is -4 plus 0.25, as divmod(-3.75, 1) shows. The :.2f only trims noise (formatting decimal places with f-strings).

Method 3: Get Exact Decimal Digits in Python with the decimal Module

For exact digits, like cents on the same receipt rows, build a Decimal from the number’s string and take % 1:

from decimal import Decimal
from prices import PRICES

for price in PRICES:
    d = Decimal(str(price))
    frac = d % 1
    cents = int(frac * 100)
    print(f"{price:>8}  decimal {frac:>5}  cents {cents}")

Output:

   19.99  decimal  0.99  cents 99
   249.5  decimal   0.5  cents 50
   -3.75  decimal -0.75  cents -75
     7.0  decimal   0.0  cents 0
 1234.56  decimal  0.56  cents 56
VS Code output of the Python decimal module returning exact fractional parts and cents for each receipt total
No float noise: 19.99 gives exactly 0.99, or 99 cents.

How does this code work?

Decimal(str(19.99)) keeps the digits you see, so % 1 is exactly 0.99, and the refund keeps its sign. I use this for money in Python, then print 2 decimal places on the receipt.

Method 4: Split the Number String at the Decimal Point

If the value is still text, say from a pandas CSV import, str.partition(".") splits it without float math:

for text in ["19.99", "249.50", "7", "1e-07"]:
    whole, _, digits = text.partition(".")
    print(f"{text:>7} -> integer {whole!r:>8}  digits {digits!r}")

Output:

  19.99 -> integer     '19'  digits '99'
 249.50 -> integer    '249'  digits '50'
      7 -> integer      '7'  digits ''
  1e-07 -> integer  '1e-07'  digits ''
VS Code output of Python str.partition splitting number strings at the decimal point, failing on 1e-07
249.50 keeps its trailing zero, but 1e-07 has no dot.

How does this code work?

You get the text before the dot, the dot and the rest. Need math? Start with converting a string to a float in Python.

Which Way to Get the Decimal Part in Python Should You Pick?

Method-3.75 givesBest for
math.modf(x)-0.75Both parts, signed (my default)
x % 10.25Positive numbers only
x - int(x)-0.75No import
Decimal(str(x)) % 1-0.75Exact cents
str.partition(".")digits as textValues that are still text

I’d start with modf(). NumPy’s np.modf() splits whole arrays, and Python’s round() function covers tidy display values.

What to Watch for with Python Fractional Parts

You’ll hit three Python fractional-part traps:

  • Float noise: 19.99 gives 0.9899999999999984; round it or use Decimal.
  • Negative numbers: % 1 turns -3.75 into 0.25; use modf().
  • Scientific notation: “1e-07” has no dot; parse it first (converting scientific notation strings).

Want more number tricks? You’ll like these:

Questions About the Decimal Part of a Number in Python

How do I get only the decimal part of a number in Python?

Use math.modf(x)[0]; it keeps the sign.

How do I get the integer part of a float in Python?

Use int(x) or math.modf(x)[1]. Both cut toward zero.

Why does 19.99 % 1 print 0.9899999999999984?

19.99 has no exact binary form. Round it or use Decimal.

Why does -3.75 % 1 return 0.25 in Python?

Because % floors: -3.75 is -4 plus 0.25. Use math.modf().

How do I get the decimal digits as cents?

Use int(Decimal(str(x)) % 1 * 100). For 19.99 that gives 99.

Where is math.modf() documented?