# Split a number into integer and decimal parts in Python Last updated: Jul 13, 2022 Photo from Unsplash

## Split a number into integer and decimal parts in Python#

Use the `math.modf()` method to split a number into integer and decimal parts, e.g. `result = math.modf(my_num)`. The `math.modf()` method returns the fractional and integer parts of the provided number.

main.py
```Copied!```import math

my_num = 1.3588

# ✅ split number into integer and decimal parts
result = math.modf(my_num)
print(result)  # 👉️ (0.3588, 1.0)

print(result)  # 👉️ 0.3588
print(result)  # 👉️ 1.0

# ✅ unpack decimal and integer values

dec, integer = result
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 carry the sign of the provided number and are floats.
main.py
```Copied!```import math

my_num = -1.3588

result = math.modf(my_num)
print(result)  # 👉️ (-0.3588, -1.0)

print(result)  # 👉️ -0.3588
print(result)  # 👉️ -1.0

# ✅ unpack decimal and integer values

dec, integer = result
print(dec)  # 👉️ -0.3588
print(integer)  # 👉️ -1.0
``````

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

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

main.py
```Copied!```import math

my_num = 1.3588

result = math.modf(my_num)
print(result)  # 👉️ (0.3588, 1.0)

dec = result
print(dec)  # 👉️ 0.3588

integer = int(result)
print(integer)  # 👉️ 1
``````

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

To split a number into integer and decimal parts:

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
```Copied!```my_num = -1.3588

dec = my_num % 1
print(dec)  # 👉️ 0.3588

integer = my_num // 1
print(integer)  # 👉️ 1.0
``````

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
```Copied!```print(1.3588 % 1)  # 👉️ 0.3588
``````

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

main.py
```Copied!```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
```Copied!```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.

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
```Copied!```my_num = 1.3588

result = divmod(my_num, 1)
print(result)  # 👉️ (1.0, 0.3588)

integer = int(result)
print(integer)  # 👉️ 1

dec = result
print(dec)  # 👉️ 0.3588
``````

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 from 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
```Copied!```my_num = -1.3588

result = divmod(my_num, 1)
print(result)  # 👉️ (-2.0, 0.6412)

integer = int(result)
print(integer)  # 👉️ -2

dec = result
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.

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