Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining whether an value is a whole number in Python

Tags:

I would like to determine if a numeric value in Python is a whole number. For example, given:

y = x / 3 

I want to distinguish between values of x which are evenly divisible by 3 those which are not.

like image 908
johntheripper Avatar asked Jun 04 '11 23:06

johntheripper


People also ask

How do you check if a value is a whole number in Python?

Here's another method: x = 1/3 # insert your number here print(x - int(x) == 0) # True if x is a whole number, False if it has decimals.

How do you know if a number is a whole number?

function number_test(n) { var result = (n - Math. floor(n)) !== 0; if (result) return 'Number has a decimal place. '; else return 'It is a whole number.

How do you check if a string is a whole number Python?

To check if a string is integer in Python, use the isdigit() method. The string isdigit() is a built-in Python method that checks whether the given string consists of only digits. In addition, it checks if the characters present in the string are digits or not.

How do you check if a float value is a whole number?

is_integer() method to check if a float is a whole number, e.g. result = (3.00). is_integer() . The float. is_integer method will return True if the float is a finite, whole number, otherwise it returns False .


1 Answers

Integers have no decimals. If you meant "check if a number got decimals in Python", you can do:

not float(your_number).is_integer() 
like image 135
Artur Gaspar Avatar answered Oct 04 '22 22:10

Artur Gaspar