Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk3 min

How to Get the Decimal Part of a Number in Python

math.modf(x)[0] returns the signed fractional part of a float. Here is how it differs from % 1 and floor for negatives, and when to use Decimal.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a regular Python float, use math.modf(x). It returns (fractional_part, integer_part), both as floats, and both carry the sign of x. So math.modf(3.14)[0] is about 0.14, and math.modf(-3.14)[0] is about -0.14. The details that trip people up are negative numbers, % 1, and float rounding noise.

The quick answer

import math

fraction, whole = math.modf(3.14)
print(fraction)  # 0.14000000000000012
print(whole)     # 3.0

If you only need the fraction, write math.modf(x)[0]. Python’s math documentation describes modf as returning “the fractional and integer parts of x. Both results carry the sign of x and are floats.”

As an Amazon Associate I earn from qualifying purchases.

The same signed result comes from truncation: x - math.trunc(x). math.trunc rounds toward zero, not toward negative infinity.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Negative numbers: pick a convention first

“Fractional part” means two different things for negative inputs. Decide which one you need.

  • Signed (truncation): math.modf(-3.14)[0] or -3.14 - math.trunc(-3.14) gives about -0.14. Use this when the fraction should keep the number’s sign, so that whole + fraction rebuilds the original.
  • Floor-based, in [0, 1): x - math.floor(x) gives about 0.86 for -3.14. Use this for things like wrapping a phase or position into a unit interval.

math.floor rounds toward negative infinity, and that is the whole difference.

Why % 1 is not the same as modf

For built-in floats, % follows the floor-division remainder convention. With a positive divisor, the result is nonnegative. So -3.14 % 1 is about 0.86, matching the floor-based formula, not modf. For positive numbers all of these agree, which is why the difference only shows up once negatives arrive.

Comparing the approaches

Approach Result for -3.14 Best fit Caution
math.modf(x)[0] about -0.14 (signed) Clean split into fraction and whole Returns a tuple; float approximation applies
x - math.trunc(x) about -0.14 (signed) Explicit truncation-based fraction Think about input type and special values
x - math.floor(x) about 0.86 (nonnegative) Fraction in [0, 1) Different meaning for negatives
x % 1 about 0.86 (float remainder rule) When remainder semantics are wanted Not interchangeable with modf for negatives
Decimal arithmetic -0.14 exactly, with % 1 or x - x // 1 Base-10 exact input Don’t mix with float; context and special values matter

Why you see 0.14000000000000012

Python floats are binary. Many decimal fractions, including 3.14, have no exact finite binary form, so the stored value is a close approximation. Subtracting the whole part exposes that tiny error. math.modf is working correctly. The Python tutorial recommends the decimal module when you need exact decimal representation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If you only need to display the result, round it for output: round(fraction, 2) or f"{fraction:.2f}".

Exact decimal fractions with Decimal

Build the Decimal from a string, not a float, or the float’s error comes along with it.

from decimal import Decimal

x = Decimal("3.14")
fraction = x - x // 1
print(fraction)  # 0.14

Decimal // truncates toward zero, and Decimal % returns a result with the sign of the dividend. So Decimal("-3.14") % 1 is -0.14, which is the opposite of the float behavior above. Decimal also supports NaN and infinities, and operations on them may signal exceptions, so the finite examples here are not universal.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Getting the digits after the point

If you want the digits themselves (for example 14), the fraction as a float is the wrong tool because of the binary noise. Work from a decimal string instead:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from decimal import Decimal

d = Decimal("3.14")
digits = str(d).split(".")[1]  # "14"

This assumes the value has a fractional part in plain notation. Handle integers (no “.”), negative signs and exponent notation if your input can contain them.

Edge cases

  • Very large floats have no fractional bits, because the spacing between representable values is wider than 1. Their fractional part is 0.0. For example, math.modf(1e16) returns (0.0, 1e16).
  • Result order is fraction first, integer second, the reverse of what many people expect.
  • Both outputs of modf are floats, so the whole part is 3.0, not 3. Wrap it in int() if you need an integer.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.