How To Remove The Division Decimal In Python

How to remove the division decimal in Python

To remove the division decimal in Python, we can use the arithmetic operator // or use the modf() method. Let follow the article below.

How to remove the division decimal in Python

Use arithmetic operator

Use the arithmetic operator ‘//’ to divide two numbers. The effect of the arithmetic operator ‘//’ is to round off, which means that the result is not decimal.

Example:

  • Initialize 2 variables containing two integers.
  • Use the arithmetic operator ‘//’ to divide two integers. Results are returned in rounded form (no decimals).
myNumber1 = 18
myNumber2 = 4

# Use the arithmetic operator ‘//’ to remove the division decimal
result = myNumber1 // myNumber2 

print('Division result without decimals :', result)

Output:

Division result without decimals : 4

Use the int() method

The int() function can convert the division result to a non-decimal form.

Syntax:

int(value, base)

Parameters:

  • value: number or string you want to convert to an integer.
  • base: number representation number format.

The int() method returns:

  • An integer is if the parameter is a number or a string.
  • Returns 0 if there are no parameters.

Example:

  • Initialize 2 variables containing two integers.
  • Use the ‘/’ arithmetic operator to take the quotient of two numbers, then use the int() function to return the result without decimals.
myNumber1 = 18
myNumber2 = 4

resultDecimal = myNumber1 / myNumber2 # 4.5

# Use the int() to remove the division decimal
resultNumber = int(resultDecimal)

print('Division result:', resultDecimal)
print('Division result without decimals :', resultNumber)

Output:

Division result: 4.5
Division result without decimals : 4

Use the modf() method

You can use the modf() function to remove the decimal part. modf() function is available in module ‘math’.

Syntax:

math.modf(x)

Parameters:

  • x: a value you want to convert.

The math.modf() function will return the decimal and integer part of x. Both the integer part and the decimal part are returned as the float.

Example:

import math

myNumber1 = 18
myNumber2 = 4

resultDecimal = myNumber1 / myNumber2 # 4.5

# Using math.modf() to remove the division decimal
resultNumber = math.modf(resultDecimal) # (0.5, 4.0)

print('Division result:', resultDecimal)
print('Decimal part:', resultNumber[0])
print('Integer part:', int(resultNumber[1]))

Output:

Division result: 4.5
Decimal part: 0.5
Integer part: 4

Use the round() method

The round() function in Python rounds a given number, returning it as a floating point number with the specified number of digits after the comma. The default number after the comma is 0, which means the function will return the nearest integer.

Syntax:

round(value, digits)

Parameters:

  • value: the value you want to round.
  • digits: number of digits after the decimal point. The default is 0.

Example:

import math

myNumber1 = 18
myNumber2 = 4

resultDecimal = myNumber1 / myNumber2 # 4.5

# Using round() to remove the division decimal
resultNumber = round(resultDecimal)

print('Division result:', resultDecimal)
print('Division result without decimals :', resultNumber)

Output:

Division result: 4.5
Division result without decimals : 4

Result of division: myNumber1 / myNumber2 = 4.5. round() function rounds to 4.

Summary

Here are the solutions that can help you remove the division decimal 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 *