How to Remove Decimal in PythonWe 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: <class 'int'> <class 'float'> <class 'complex'> 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.
Let’s discuss each one of them in detail- Using trunc() FunctionIn 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 <class 'int'> 21 <class 'int'> 182 <class 'int'> 211 <class 'int'> 19 <class 'int'> Explanation: Let’s have a look at the explanation of the above program-
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: <class 'float'> <class 'float'> <class 'float'> 523 21 182 <class 'int'> Explanation: Let’s understand what we have done here-
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-
ConclusionIn 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. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/courses/263384.html