# Split a Float into Integer and Decimal parts in Python

Last updated: Apr 9, 2024
4 min

## #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
```Copied!```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
``````

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

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

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
```Copied!```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
``````

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
```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.

## #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
```Copied!```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 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
```Copied!```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
```Copied!```a_float = 1.03588

integer = int(a_float)
print(integer)  # ๐๏ธ 1

dec = a_float - integer
print(dec)  # ๐๏ธ 0.03587999999999991
``````

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.