Warning: session_start(): open(/tmp/sess_5c0cc55b278920f920b8f7707705c17f, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Split A Number Into Integer And Decimal Parts In Python - LearnShareIT

How To Split A Number Into Integer And Decimal Parts In Python

Split a number into integer and decimal parts in Python

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:

Leave a Reply

Your email address will not be published. Required fields are marked *