How to Get the Decimal Part of a Number in Python?

In this tutorial, I will explain how to get the decimal part of a number in Python. As a Python developer, I recently faced this issue while developing a financial application for a client in New York City, and I found several effective methods to handle this task. Let’s get into the details.

Methods to Extract the Decimal Part of a Number in Python

Python offers several ways to extract the decimal part of a number. Here, I’ll cover the most common methods:

Method 1. Use the Modulo Operator

The modulo operator (%) is a simple and effective way to extract the decimal part of a float. Here’s how you can do it:

def get_decimal_part(number):
    return number % 1

# Example
number = 123.456
decimal_part = get_decimal_part(number)
print(f"The decimal part of {number} is {decimal_part}")

Output:

The decimal part of 123.456 is 0.45600000000000307

I have executed the above example and added the screenshot below.

Decimal Part of a Number in Python

In this example, 123.456 % 1 returns 0.456 , which is the decimal part of the number.

Read How to Use the trim() Function in Python?

Method 2. Use the math.modf() Function

The math.modf() function splits a floating-point number into its integer and fractional parts. This function is part of Python’s standard library.

import math

def get_decimal_part(number):
    fractional, integer = math.modf(number)
    return fractional

# Example
number = 789.123
decimal_part = get_decimal_part(number)
print(f"The decimal part of {number} is {decimal_part}")

Output:

The decimal part of 789.123 is 0.1230000000000473

I have executed the above example and added the screenshot below.

Get the Decimal Part of a Number in Python

In this example, math.modf(789.123) returns (0.123, 789.0), where 0.123 is the fractional part.

Check out How to Use the strip() Function in Python?

Method 3. Use String Manipulation

Another method involves converting the number to a string and then manipulating the string to extract the decimal part.

def get_decimal_part(number):
    str_number = str(number)
    if '.' in str_number:
        return '0.' + str_number.split('.')[1]
    else:
        return '0.0'

# Example
number = 456.789
decimal_part = get_decimal_part(number)
print(f"The decimal part of {number} is {decimal_part}")

Output:

The decimal part of 456.789 is 0.789

I have executed the above example and added the screenshot below.

How to Get the Decimal Part of a Number in Python

In this example, the number is converted to a string, split at the decimal point, and the fractional part is reconstructed as a string.

Read How to Implement and Use the hash() Functions in Python?

Method 4. Use the Decimal Module

Python’s decimal module provides support for fast and correctly-rounded decimal floating point arithmetic. This method is particularly useful for financial applications.

from decimal import Decimal

def get_decimal_part(number):
    decimal_number = Decimal(str(number))
    return decimal_number - int(decimal_number)

# Example
number = 123.456
decimal_part = get_decimal_part(number)
print(f"The decimal part of {number} is {decimal_part}")

In this example, Decimal(str(number)) ensures that the number is treated with high precision, and subtracting the integer part gives the fractional part.

Check out How to Use the map() Function in Python?

Examples

Let’s see some real-world examples where extracting the decimal part is necessary.

1. Financial Application: Calculate Change

Imagine you’re developing a point-of-sale system for a retail store in Los Angeles. You need to calculate the change to be given to the customer, and you want to display the cents separately.

def calculate_change(total_amount, payment):
    change = payment - total_amount
    cents = get_decimal_part(change)
    return change, cents

# Example
total_amount = 19.95
payment = 20.00
change, cents = calculate_change(total_amount, payment)
print(f"Total change: ${change:.2f}, Cents: {cents:.2f}")

In this example, the get_decimal_part function helps isolate the cents from the total change.

Read Difference Between *args and **kwargs in Python

2. Scientific Data Analysis: Precision Measurements

Suppose you’re working on a scientific project in a lab in Houston, and you need to record the fractional part of measurements precisely.

measurements = [1.234, 2.345, 3.456, 4.567]
fractional_parts = [get_decimal_part(m) for m in measurements]
print(f"Fractional parts of measurements: {fractional_parts}")

This example shows how to extract the fractional parts of a list of measurements.

Check out How to Use the randint() Function in Python?

Conclusion

In this tutorial, I have explained how to get the decimal part of a number in Python. I discussed several methods to achieve this task, using the modulo operator, using math.modf() function, using string manipulation and using a decimal module. We also discussed real-time examples of financial application and scientific data analysis.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.