How to remove decimal numbers in Python [5 Examples]

Do you want to learn how to remove decimal numbers in Python? You have come to the right place.

In this Python tutorial, I will describe the different methods to remove decimal numbers in Python with some illustrative examples.

Keep reading this Python article to learn how to remove decimal numbers with different methods.

  • Using int() function
  • Using math.trunc() method
  • Using math.ceil() method
  • Using round() function
  • Using string formatting and methods : using split() function, using %d placeholder

Python program to remove the decimal numbers in Python using the int() function

Python provides a built-in function int() to typecast a value to an integer and removes the decimal portion of the number. To remove decimal places in Python, we will pass a float value with a decimal part to this function, which will remove decimal places in Python.

This is the full source code of Python to remove the decimal numbers using the int() function.

marks_obtained = 90.44
cut_off = 140
negative_marking = -1.5

print(int(marks_obtained))
print(int(cut_off))
print(int(negative_marking))

print(type(marks_obtained))
print(type(cut_off))
print(type(negative_marking))

Output:

90
140
-1
<class 'float'>
<class 'int'>
<class 'float'>

Refer to the screenshot below, and it will display the output of the Python program to remove decimal numbers using the int() function.

Remove decimal numbers using int() function in Python

How to remove decimal numbers in Python using math.trunc() method

We can remove decimal places in Python using the trunc() function from the math library. This function takes a float value and removes the decimal part from the same, returning the integer value.

READ:  Could not convert string to float in Python

Note: This method will not round the number up/down to the nearest integer but remove the decimal.

This is the full source code of Python to remove decimal numbers using the math module with the trunc() function.

import math

num_one = 1.53
num_two = -6.12
num_three = 100.234

print(math.trunc(num_one))
print(math.trunc(num_two))
print(math.trunc(num_three))

Output:

1
-6
100

The screenshot below shows the output of the Python program to remove decimal numbers using the math module with the trunc() function.

Python program to remove decimal numbers using the math module with trunc() function

Python program to remove decimal numbers in Python using math.ceil() method

Python provides the math module to perform mathematical tasks. The ceil() function in Python is used to round a number up to the nearest integer, if necessary, and returns the result.

This is the complete Python program to remove decimal numbers using math.ceil method in Python.

import math


Chicago_latitude = 41.881832
Chicago_longitude =  -87.623177

print(math.ceil(Chicago_longitude))

print(math.ceil(Chicago_latitude))

Output:

42
-87

Refer to the screenshot below, it will display the output of the Python program to remove decimal numbers using the math.ceil method.

Python program to remove decimal numbers using the math.ceil method

How to remove decimal numbers in Python using the round() function

The built-in round() function provided by Python is capable of rounding off the float value to the nearest integer value. If the decimal places to be rounded are not specified, it is considered 0.

Below is the Python code that would remove decimal numbers using the round() function.

input_num1 = 1.53
print(round(input_num1))

input_num2 = -11.15
print(round(input_num2))

input_num3 = 128.2341
print(round(input_num3))

Output:

2
-11
128

You can refer to the image below to see the out of the Python program to remove decimal numbers using the round() function.

How to remove decimal number using the round() function in Python

How to remove decimal numbers using string formatting and methods

In Python, there are two ways of using string formatting and methods to remove decimal numbers in Python. Everything returned in this section will be a string.

  • Using split() function
  • Using % placeholder
READ:  How to Split a List in Python By Comma

Python program to remove decimal numbers using split() function

The split() function in Python only works on strings after breaking the given string by the specified separator.

Python source code to remove decimal numbers using the split function.

decimal_value = [98.19, 56.8, 25.6, -52.0] 
empty_list = []

for each in decimal_value: 
    empty_list.append(str(each).split('.')[0]) 
  
final_list = [int(i) for i in empty_list] 
print(final_list)

It iterates over each element in a list or iterable called decimal_value. For each element in decimal_value, it converts it to a string (str(each)).

Splits it at the decimal point (‘.’), then appends the part before the decimal point to a list called empty_list.

empty_list.append(str(each).split('.')[0]) 

Output:

Refer to the image to see the output of the Python program to remove decimal numbers using the split() function.

How to remove decimal number using the round() function in Python

Python program to remove decimal numbers using %d placeholder

This is another way to remove decimal numbers in Python using the %d placeholder. This is used to round off the given numbers and the numbers after the decimal are excluded. It will only extract the integer parts.

Here is the full code of Python to remove decimal numbers using the %d placeholder.

temperature = 29.8
print('%d'%temperature)
print(type(temperature))

Output:

29
<class 'float'>

The screenshot below shows the output of the Python program to remove decimal numbers using the %d placeholder.

Python program to remove decimal numbers using the %d placeholder

Conclusion

This tutorial provides information about how to remove decimal numbers in Python using different methods like using the int() function, math.trunc() method, math.ceil() method, round() function, string formatting method (using split() function and using %d placeholder), split() function, and %d placeholder with examples.

I hope you understand all of them.

READ:  Python find number in String [4 Methods]

You may also like to read: