How To Get Number Without Decimal Places In Python

Get number without decimal places in Python

To get number without decimal places in Python, you can use Python built-in functions like int(), round(), or math.trunc() function. Read the following article for more details.

Get number without decimal places in Python

Use the int() function

Syntax:

int(value, base)

Parameters:

  • value: The number or string you want to convert to an integer.
  • base: Number represents the number format. The default is 10.

The int() function returns:

  • An integer from the number or string you pass in.
  • Returns 0 if the parameter is left blank.

Example:

  • Declare a variable containing decimals.
  • Using the int() function, it will truncate floating numbers to zero returning an integer representing the number without decimals.
floatVar = 1.2

# Use the int() function to take an integer
intVar = int(floatVar)
print('The Integer is:', intVar)

Output:

The Integer is: 1

Use the round() function

Syntax:

round(number[, ndigits])

Parameters:

  • number: Number you want to round.
  • ndigits: The number of digits you want after the decimal point.

Return value from the round() function:

  • If there are no ndigits, the function returns the nearest integer.
  • If two arguments are provided, the function returns a number with ndigits decimal places after the decimal point.

Example:

  • Declare a variable containing decimals.
  • The round() function will round a number up or down to an integer.
floatVar = 1.2

# Use the round() function to take an integer
intVar = round(floatVar)
print('The Integer is:', intVar)

Output:

The Integer is: 1

Separate the integer part and the decimal part

I can separate integer and decimal parts using split(), int() and list comprehensions.

Example:

  • Separate the integer part and the decimal part.
  • Then run them through the int() function to become an integer.
  • Note: Float has no split attribute, so you need to initialize a string.
numberStr = '1.2'

# Separate the integer part and the decimal part
integerPart , decimalPart = [int(i) for i in numberStr.split('.')]

print('The integer part is:', integerPart)
print('The decima part is:', decimalPart)

Output:

The integer part is: 1
The decimal part is: 2

Use the math.trunc function

Syntax:

math.trunc(value)

Parameters:

  • value: Real numbers need to remove the decimal part.

The math.trunc function returns an integer (decimals will be removed).

Example:

  • Declare a variable containing decimals.
  • Using the math.trunc() function will truncate floating numbers to zero returning an integer representing the number without decimals.
import math

floatVar = 1.2

# Use the math.trunc() function to take an integer
intVar = math.trunc(floatVar)
print('The Integer is:', intVar)

Output:

The Integer is: 1

Summary

How to get number without decimal places in Python is a pretty easy topic. You can use 1 of 4 ways to do this. If you have any comments about the article, leave us a comment below.

Maybe you are interested:

Leave a Reply

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