7

I'm using this to check if a variable is numeric, I also want to check whether it's a floating point number.

if(width.isnumeric() == 1)
Forge
  • 6,538
  • 6
  • 44
  • 64
Harry Krek
  • 91
  • 1
  • 1
  • 2
  • 1
    Would you want `3` and `3.5` to go in the same check? – zondo Mar 05 '16 at 00:33
  • `isinstance(width, type(1.0))` works in python 2.7 – Garrett R Mar 05 '16 at 00:34
  • 1
    Possible duplicate of [Check if a number is int or float](http://stackoverflow.com/questions/4541155/check-if-a-number-is-int-or-float) – LinkBerest Mar 05 '16 at 00:35
  • 3
    @JGreenwell: `width.isnumeric()` is a hint that `width` is a string. He is not checking to see if a real number is a float; he is checking to see if a *string* could be converted to a float. – zondo Mar 05 '16 at 00:38
  • @zondo why would that be a hint? I've used varying values of width (from float to int) for a number of projects I've developed - besides [at least one of the answers](http://stackoverflow.com/questions/4541155/check-if-a-number-is-int-or-float/4541207#4541207) is fine for checking a string – LinkBerest Mar 05 '16 at 00:49
  • @JGreenwell If `width` is a float, `width.isnumeric` doesn't exist. `isnumeric` is a string method. – zondo Mar 05 '16 at 00:51
  • true, but that question includes answers for checking both int types and str types so it still a good fit - though not exact and answers here are good too (so more a related/linked at this point anyway). – LinkBerest Mar 05 '16 at 00:59
  • Your question is not at all clear --- note all the comments trying to guess what your inputs (and input types) are, what outputs you want, and how you intend to handle edge cases. **These are all things that _you_ should tell _us_**, not things we should be guessing at. What _exactly_ are you trying to do, and _why_? What kinds of inputs do you expect? What outputs do you want from those inputs? What do you mean by "floating point number"? `1.0` is of type `float`, but it has an integer value (as does `fractions.Fraction(10, 5)`) --- are these "floating point" for your purposes, or not? – Kevin J. Chase Mar 05 '16 at 03:34
  • @zondo While I agree that Harry Krek probably intended `width` to be a string, it's really not our job to play "guess the spec" with questions like this. If the question is this unclear to people who have already found it, it will be of _zero_ use to later visitors who are searching for answers to the same question (whatever it turns out to be) --- they'll never find it. – Kevin J. Chase Mar 05 '16 at 03:40

3 Answers3

17

The easiest way is to convert the string to a float with float():

>>> float('42.666')
42.666

If it can't be converted to a float, you get a ValueError:

>>> float('Not a float')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 'Not a float'

Using a try/except block is typically considered the best way to handle this:

try:
  width = float(width)
except ValueError:
  print('Width is not a number')

Note you can also use is_integer() on a float() to check if it's an integer:

>>> float('42.666').is_integer()
False
>>> float('42').is_integer()
True
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
7
def is_float(string):
  try:
    return float(string) and '.' in string  # True if string is a number contains a dot
  except ValueError:  # String is not a number
    return False

Output:

>> is_float('string')
>> False
>> is_float('2')
>> False
>> is_float('2.0')
>> True
>> is_float('2.5')
>> True
Forge
  • 6,538
  • 6
  • 44
  • 64
0

Here another solution without "try" and which is returning a truth-value directly. Thanks to @Cam Jackson. I found this solution here: Using isdigit for floats?

The idea is to remove exactly 1 decimal point before using isdigit():

>>> "124".replace(".", "", 1).isdigit()
True
>>> "12.4".replace(".", "", 1).isdigit()
True
>>> "12..4".replace(".", "", 1).isdigit()
False
>>> "192.168.1.1".replace(".", "", 1).isdigit()
False
Thomas R
  • 1,067
  • 11
  • 17