Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a float value is within a certain range and has a given number of decimal digits?

How to check if a float value is within a range (0.50,150.00) and has 2 decimal digits?

For example, 15.22366 should be false (too many decimal digits). But 15.22 should be true.

I tried something like:

data= input()
if data in range(0.50,150.00):
   return True
like image 358
user3841581 Avatar asked Aug 23 '14 20:08

user3841581


People also ask

How do you check if a float number is within a range in Python?

The easiest way is to first convert the decimal to string and split with '. ' and check if the length of the character. If it is >2 then pass on. i.e. Convert use input number to check if it is in a given range.

How do you find the range of a float?

Range of floats using numpy.linspace() to get a range of float numbers. The numpy. linspace() returns number spaces evenly w.r.t interval. Similar to arange , but instead of step, it uses a sample number.

How do you check if a float is a number?

Check if the value has a type of number and is not an integer. Check if the value is not NaN . If a value is a number, is not NaN and is not an integer, then it's a float.

Can float values be decimals?

If doing math with floats, you need to add a decimal point, otherwise it will be treated as an int. See the Floating point constants page for details. The float data type has only 6-7 decimal digits of precision. That means the total number of digits, not the number to the right of the decimal point.


1 Answers

Is that you are looking for?

def check(value):
    if 0.50 <= value <= 150 and round(value,2)==value:
        return True
    return False

Given your comment:

i input 15.22366 it is going to return true; that is why i specified the range; it should accept 15.22

Simply said, floating point values are imprecise. Many values don't have a precise representation. Say for example 1.40. It might be displayed "as it":

>>> f = 1.40
>>> print f
1.4

But this is an illusion. Python has rounded that value in order to nicely display it. The real value as referenced by the variable f is quite different:

>>> from decimal import Decimal
>>> Decimal(f)
Decimal('1.399999999999999911182158029987476766109466552734375')

According to your rule of having only 2 decimals, should f reference a valid value or not?

The easiest way to fix that issue is probably to use round(...,2) as I suggested in the code above. But this in only an heuristic -- only able to reject "largely wrong" values. See my point here:

>>> for v in [ 1.40,
...            1.405,
...            1.399999999999999911182158029987476766109466552734375,
...            1.39999999999999991118,
...            1.3999999999999991118]:
...     print check(v), v
...
True 1.4
False 1.405
True 1.4
True 1.4
False 1.4

Notice how the last few results might seems surprising at first. I hope my above explanations put some light on this.


As a final advice, for your needs as I guess them from your question, you should definitively consider using "decimal arithmetic". Python provides the decimal module for that purpose.

like image 76
Sylvain Leroux Avatar answered Oct 20 '22 21:10

Sylvain Leroux