Split a Float into Integer and Decimal parts in Python

Borislav Hadzhiev

Last updated: Apr 9, 2024

Reading timeยท4 min

- Split a Float into Integer and Decimal parts in Python
- Split a number into integer and decimal parts using modulo operator
- Split a number into integer and decimal parts using divmod

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

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 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 code for this article is available on GitHub

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

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

The code for this article is available on GitHub

Alternatively, you can use the `%`

operator and
floor division `//`

.

This is a two-step process:

- Use floor division to get the integer part of the number by dividing by
`1`

, e.g.`num // 1`

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

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

- The result of dividing the first argument by the second.
- 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.

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

- Converting the float to an integer (to drop the decimal).
- 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.

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

- Round a Float to 1, 2 or 3 Decimal places in Python
- How to Remove the trailing Zeros from a Decimal in Python
- Count the decimal places of a Float in Python
- Remove the Decimal part from a Float in Python
- Format number with comma as thousands separator in Python
- Add zeros to a Float after the Decimal in Python