How do you get no decimals in python?

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.

  1. Using trunc[] function
  2. Using int[]
  3. 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-

  1. Since we have to use the trunc[] function, we have imported the math module.
  2. We have provided five different decimal values to five variables and checked their type after they are passed in the trunc[] function.
  3. 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-

  1. In the first step, we have provided float values to three variables and checked their type.
  2. After this, we have passed each variable to int[] and stored them to a new variable.
  3. Finally, we have printed the values stored in these variables.
  4. 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-

  1. In the first step, we have created a list that contains all the decimal values.
  2. After this, we have declared an empty list and appended the values in it.
  3. In the next step, we have taken each element from that list and passed it into an int[].
  4. 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

How do you round to no decimal places in Python?

Python has a built-in round[] function that takes two numeric arguments, n and ndigits , and returns the number n rounded to ndigits . The ndigits argument defaults to zero, so leaving it out results in a number rounded to an integer.

How do you print a variable without a decimal in Python?

Solution 1: int[] Python's built-in function int[x] converts any float number x to an integer by truncating it towards 0. For example, positive int[1.999] will be truncated to 1 and negative int[-1.999] to -1 .

How do you get rid of decimals?

Step 1: Write down the decimal divided by 1. Step 2: Multiply both top and bottom by 10 for every number after the decimal point. [For example, if there are two numbers after the decimal point, then use 100, if there are three then use 1000, etc.] Step 3: Simplify [or reduce] the Rational number.

How do I remove a decimal from a Dataframe in Python?

Lets use the dataframe. round[] function to round off all the decimal values in the dataframe to 3 decimal places. Output : Example #2: Use round[] function to round off all the columns in dataframe to different places.

Chủ Đề