Split a Float into Integer and Decimal parts in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Table of Contents

  1. Split a Float into Integer and Decimal parts in Python
  2. Split a number into integer and decimal parts using modulo operator
  3. Split a number into integer and decimal parts using divmod

# Split a Float into Integer and Decimal parts in Python

Use the math.modf() method to split a number into integer and decimal parts.

The math.modf() method returns the fractional and integer parts of the provided number.

main.py
import math my_num = 1.3588 result = math.modf(my_num) print(result) # ๐Ÿ‘‰๏ธ (0.3588, 1.0) print(result[0]) # ๐Ÿ‘‰๏ธ 0.3588 print(result[1]) # ๐Ÿ‘‰๏ธ 1.0

split float into integer and decimal parts

The code for this article is available on GitHub

You can also use unpacking to assign the decimal and integer parts to variables.

main.py
import math my_num = 1.3588 dec, integer = math.modf(my_num) print(dec) # ๐Ÿ‘‰๏ธ 0.3588 print(integer) # ๐Ÿ‘‰๏ธ 1.0

We used the math.modf() method to split a number into integer and decimal parts.

The math.modf() method returns the fractional and integer parts of the provided number.

# The fractional and integer parts are of type float

The fractional and integer parts carry the sign of the provided number and are floats.

main.py
import math my_num = -1.3588 result = math.modf(my_num) print(result) # ๐Ÿ‘‰๏ธ (-0.3588, -1.0) print(result[0]) # ๐Ÿ‘‰๏ธ -0.3588 print(result[1]) # ๐Ÿ‘‰๏ธ -1.0 # ----------------------------------------- # โœ… Unpack decimal and integer values dec, integer = result print(dec) # ๐Ÿ‘‰๏ธ -0.3588 print(integer) # ๐Ÿ‘‰๏ธ -1.0

the fractional and integer parts are of type float

The code for this article is available on GitHub

Notice that both of the values in the tuple are floats.

# Converting the integer part to an int after splitting

If you need to convert the second tuple element to an integer, use the int() class.

main.py
import math my_num = 1.3588 result = math.modf(my_num) print(result) # ๐Ÿ‘‰๏ธ (0.3588, 1.0) dec = result[0] print(dec) # ๐Ÿ‘‰๏ธ 0.3588 integer = int(result[1]) print(integer) # ๐Ÿ‘‰๏ธ 1

converting the integer part to an int after splitting

The code for this article is available on GitHub

Alternatively, you can use the % operator and floor division //.

# Split a number into integer and decimal parts using modulo operator

This is a two-step process:

  1. Use floor division to get the integer part of the number by dividing by 1, e.g. num // 1.
  2. Use the modulo % operator to get the fractional part by getting the remainder after dividing by 1, e.g. num % 1.
main.py
my_num = -1.3588 dec = my_num % 1 print(dec) # ๐Ÿ‘‰๏ธ 0.3588 integer = my_num // 1 print(integer) # ๐Ÿ‘‰๏ธ 1.0
The code for this article is available on GitHub

The modulo (%) operator returns the remainder from the division of the first value by the second.

When we use the modulo operator to divide a number by 1, the remainder is the fractional part.

main.py
print(1.3588 % 1) # ๐Ÿ‘‰๏ธ 0.3588

You can use floor division to get the integer part of a number.

main.py
my_num = -1.3588 integer = my_num // 1 print(integer) # ๐Ÿ‘‰๏ธ 1.0 dec = my_num % 1 print(dec) # ๐Ÿ‘‰๏ธ 0.3588
The result of using the floor division // operator is that of a mathematical division with the floor() function applied to the result.

Dividing a number by 1 and rounding down, gives us the integer part of the number.

However, if you decide to use this approach, note that it doesn't handle negative numbers as you would expect.
main.py
my_num = -1.3588 dec = my_num % 1 print(dec) # ๐Ÿ‘‰๏ธ 0.6412 integer = my_num // 1 print(integer) # ๐Ÿ‘‰๏ธ -2.0

If you have to handle negative numbers, use the math.modf() method instead.

# Split a number into integer and decimal parts using divmod

You might also see examples online that use the divmod() function.

However, note that divmod() also doesn't handle negative numbers in a way you would expect.

main.py
my_num = 1.3588 result = divmod(my_num, 1) print(result) # ๐Ÿ‘‰๏ธ (1.0, 0.3588) integer = int(result[0]) print(integer) # ๐Ÿ‘‰๏ธ 1 dec = result[1] print(dec) # ๐Ÿ‘‰๏ธ 0.3588
The code for this article is available on GitHub

The divmod() function takes two numbers and returns a tuple containing 2 values:

  1. The result of dividing the first argument by the second.
  2. The remainder of dividing the first argument by the second.

However, the divmod() function also doesn't handle negative numbers in a way that suits our use case.

main.py
my_num = -1.3588 result = divmod(my_num, 1) print(result) # ๐Ÿ‘‰๏ธ (-2.0, 0.6412) integer = int(result[0]) print(integer) # ๐Ÿ‘‰๏ธ -2 dec = result[1] print(dec) # ๐Ÿ‘‰๏ธ 0.6412

For this reason, you should use the math.modf() method when you have to split a number into integer and decimal parts.

# Subtracting integers from floats and accuracy

You can also split a floating-point number to an integer by:

  1. Converting the float to an integer (to drop the decimal).
  2. Subtracting the integer from the float to get the decimal part.
main.py
a_float = 1.03588 integer = int(a_float) print(integer) # ๐Ÿ‘‰๏ธ 1 dec = a_float - integer print(dec) # ๐Ÿ‘‰๏ธ 0.03587999999999991
The code for this article is available on GitHub

However, notice that you might get surprising results when subtracting from a floating-point number.

The resulting number cannot be represented in binary, so the precision is lost.

This might or might not suit your use case.

I've also written an article on how to split an integer into digits.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev