how to check if a value is a floating point or not

Gary Herron gary.herron at islandtraining.com
Thu Jun 19 02:22:55 EDT 2014


On 06/18/2014 10:53 PM, nicholascannon1 at gmail.com wrote:
> I am making a calculator and i need it to support floating point values but i am using the function isnumeric to check if the user has entered an int value. I need the same for floating point types so i could implement an or in the if statement that checks the values the user has entered and allow it to check and use floating points. If you need the source code i am happy to give it to you. Thank you for your help

Your mention of *isnumeric* indicates to me that you are using Python3 
(correct?) and are wishing to test if a *string* contains characters 
that represent an int or a float (correct?).

The easiest way to test such is to just try to convert it to an int or 
float, and catch failures as an indication that it is not valid. 
Something like:

try:
   value = float(s)
except ValueError:
    ... handle invalid string ...


Gary Herron




More information about the Python-list mailing list