Get number without decimal python

a=123.45324

is there a function that will return just 123?

asked Aug 3, 2010 at 16:05

Alex GordonAlex Gordon

54.8k281 gold badges654 silver badges1037 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,3471 gold badge25 silver badges28 bronze badges

You can use the math.trunc[] function:

a=10.2345
print[math.trunc[a]]

janw

7,58610 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

Get number without decimal places in Python #

Use the int[] class to get a number without the decimal places, e.g. result = int[my_float]. The int[] class truncates floating-point numbers towards zero, so it will return an int that represents the number without the decimal places.

Copied!

my_float = 3.14 result = int[my_float] print[result] # 👉️ 3 my_float_2 = -3.14 result_2 = int[my_float_2] print[result_2] # 👉️ -3

We used the int[] class to get a number without the decimal places.

The int class returns an integer object constructed from the provided number or string argument.

If passed a floating-point number, the int[] class truncates towards zero.

This is exactly what we need because it allows us to get a negative number without the decimal places.

Copied!

my_float_2 = -3.14 result_2 = int[my_float_2] print[result_2] # 👉️ -3

You might see examples online that use the math.floor[] method in this scenario.

However, the math.floor[] method truncates toward negative infinity.

Copied!

import math print[math.floor[5.345]] # 👉️ 5 print[math.floor[-5.345]] # 👉️ -6

The math.floor method returns the largest integer less than or equal to the provided number.

An alternative to using the int[] class is to use the math.trunc[] method.

Get number without decimal places using math.trunc[] #

Use the math.trunc[] method to get a number without the decimal places, e.g. result = math.trunc[my_float]. The math.trunc[] method removes the fractional part and returns the integer part of the given number.

Copied!

import math my_float = 5.345 result_1 = math.trunc[my_float] print[result_1] # 👉️ 5 my_float_2 = -5.345 result_2 = math.trunc[my_float_2] # 👉️ -5 print[result_2]

The math.trunc method takes a number, removes its fractional part and returns its integer part.

The math.trunc[] method rounds towards zero.

Copied!

import math print[math.trunc[2.468]] # 👉️ 2 print[math.trunc[-2.468]] # 👉️ -2

Since the math.trunc[] method rounds towards zero, it handles negative numbers in a way that suits our use case.

How do you get a value without a decimal in Python?

Use the int[] class to get a number without the decimal places, e.g. result = int[my_float] . The int[] class truncates floating-point numbers towards zero, so it will return an int that represents the number without the decimal places. Copied!

How do you remove a decimal number in Python?

Type conversion in python helps to convert a decimal value number[floating number] to an integer. Thus converting float->int removes all decimals from a number.

How do you print a float value 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 .

Chủ Đề