Last updated: Apr 9, 2024
Reading timeยท4 min
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.
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.
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.
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.
If you need to convert the second tuple element to an integer, use the int() class.
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 //
.
This is a two-step process:
1
,
e.g. num // 1
.%
operator to get the fractional part by getting the
remainder after dividing by 1
, e.g. num % 1
.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.
print(1.3588 % 1) # ๐๏ธ 0.3588
You can use floor division to get the integer part of a number.
my_num = -1.3588 integer = my_num // 1 print(integer) # ๐๏ธ 1.0 dec = my_num % 1 print(dec) # ๐๏ธ 0.3588
//
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.
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.
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:
However, the divmod()
function also doesn't handle negative numbers in a way
that suits our use case.
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:
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.
You can learn more about the related topics by checking out the following tutorials: