We have worked with different kind of numbers in Python and modified their type as per our need.
In this tutorial, we will discuss how we can remove decimal in Python.
Let's start with a simple program,
Output:
Explanation:
In the above program, we have declared a,b and c as 24, 19.4, and 3+4j respectively.
On checking their type, we came to know a belongs to class 'int', b belongs to class 'float' and c belongs to class 'complex'.
Here we have to work on the float numbers, so let's list out the different methods of removing decimals from numbers.
- Using trunc[] function
- Using int[]
- Using split[]
Let's discuss each one of them in detail-
Using trunc[] Function
In the first program, we will make use of trunc[] function and remove the decimal present in the numbers.
The following program illustrates the same-
Output:
523 21 182 211 19
Explanation:
Let's have a look at the explanation of the above program-
- Since we have to use the trunc[] function, we have imported the math module.
- We have provided five different decimal values to five variables and checked their type after they are passed in the trunc[] function.
- On executing the program, it displays the required output.
Using int[]
It's time to know the second approach which is removing decimal using int[].
The program given below shows how it can be done-
Output:
523 21 182
Explanation:
Let's understand what we have done here-
- In the first step, we have provided float values to three variables and checked their type.
- After this, we have passed each variable to int[] and stored them to a new variable.
- Finally, we have printed the values stored in these variables.
- On executing this program, the expected output is displayed.
Using split[]
Finally, in the last approach, we will use the interesting split[] to obtain the integer values.
The following program illustrates the same-
Output:
The resultant list is: [523, 21, 182, 211, 19]
Explanation:
Let's have a look at the explanation of the above program-
- In the first step, we have created a list that contains all the decimal values.
- After this, we have declared an empty list and appended the values in it.
- In the next step, we have taken each element from that list and passed it into an int[].
- Finally, we have displayed the resultant list that contains the numbers without decimal.
Conclusion
In this Tutorial, we started with a general idea of the type of numbers we use in Python and then learned the various methods of removing decimals from the numbers.
a=123.45324
is there a function that will return just 123
?
asked Aug 3, 2010 at 16:05
Alex GordonAlex Gordon
54.7k281 gold badges653 silver badges1034 bronze badges
int
will always truncate
towards zero:
>>> a = 123.456
>>> int[a]
123
>>> a = 0.9999
>>> int[a]
0
>>> int[-1.5]
-1
The difference between int
and math.floor
is that math.floor
returns the number as a float, and does not truncate towards zero.
answered Aug 3, 2010 at 16:08
Mark RushakoffMark Rushakoff
241k44 gold badges401 silver badges395 bronze badges
1
Python 2.x:
import math
int[ math.floor[ a ] ]
N.B. Due to complicated reasons involving the handling of floats, the int
cast is safe.
Python 3.x:
import math
math.floor[ a ]
answered Aug 3, 2010 at 16:06
3
answered Aug 3, 2010 at 16:09
Artur GasparArtur Gaspar
4,3371 gold badge25 silver badges28 bronze badges
You can use the math.trunc[]
function:
a=10.2345
print[math.trunc[a]]
janw
7,55610 gold badges34 silver badges57 bronze badges
answered Jul 2, 2020 at 10:40
If you want both the decimal and non-decimal part:
def split_at_decimal[num]:
integer, decimal = [int[i] for i in str[num].split["."]]
return integer, decimal
And then:
>>> split_at_decimal[num=5.55]
[5, 55]
answered May 24, 2020 at 12:25
Avi VajpeyiAvi Vajpeyi
4046 silver badges13 bronze badges