To split a number into integer and decimal parts in Python, you can use the divmod() function, the modf() function, the arithmetic operators and the int() function. Follow the article to understand better.
Split a number into integer and decimal parts in Python
Use the divmod() function
You can use the divmod() function to represent a number as integers and decimals.
Syntax:
divmod(x, y)
Parameters:
- x: the dividend.
- y: the divisor.
Returns:
- Returns a tuple(q,r) where q is the quotient and r is the remainder.
- x,y are integers the return value will be (quotient, balance).
- x or y is a float, the return value is (quotient, balance).
Example:
myNumber1 = 4.5 iNumber, fNumber = divmod(myNumber1, 1) print('Integer parts', iNumber) print('Decimal part', fNumber)
Output:
Integer parts 4.0
Decimal part 0.5
Use the modf() function
Syntax:
math.modf( x )
Parameters:
- x: Numeric expression.
Example:
- Import math module.
- Initialize two numbers that can be integers, float.
- Use the modf() function to represent division as integers and decimals.
import math myNumber = 2.30 # Use the modf() function to divide numbers into decimal and integer parts separatedNum = math.modf(myNumber) print(separatedNum) print('Decimal part', separatedNum[0]) print('Integer part', separatedNum[1])
Output:
(0.2999999999999998, 2.0)
Decimal parts 0.2999999999999998
Integer part 2.0
Use the arithmetic operator
You can use arithmetic operators in conjunction with the int() function to represent numbers as integers and decimals.
The ‘%’ operator for division with remainder.
The ‘//’ operator operand division where the result is the quotient and the digits after the decimal point are removed.
Example:
# Value is an integer myNumber = 3 result1 = (int(myNumber // 1), myNumber % 1) print('Result1: ', result1) # Value is a float myNumber2 = 3.5 result2 = (int(myNumber2 // 1), myNumber2 % 1) print('Result2: ', result2)
Output:
Result1 (3, 0)
Result2 (3, 0.5)
Use the int() function
Example:
- Initialize a variable containing an integer or float value.
- Use the int() function to represent a number as an integer and decimal.
# Value is a float myNumber1 = 1.9 # Use the int() function intpart1, decimalpart1 = int(myNumber1), myNumber1 - int(myNumber1) print(myNumber1) print('Integer parts', intpart1) print('Decimal part', decimalpart1) # Value is an integer myNumber2 = 1 # Use the int() function intpart2, decimalpart2 = int(myNumber2), myNumber2 - int(myNumber2) print(myNumber2) print('Integer parts', intpart2) print('Decimal part', decimalpart2)
1.9
Integer parts 1
Decimal part 0.8999999999999999
1
Integer parts 1
Decimal part 0
Summary
Here are the solutions that can help you split a number into integer and decimal parts in Python. If you have any questions about this article, leave a comment below. I will answer your questions. Thank you for reading!
Maybe you are interested:
- Add zeros to a float after the decimal in Python
- Split an integer into digits in Python
- Convert NoneType to an Integer in Python

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java