How can I convert a string to either int or float with priority on int?

I couldn't find another answer when I wanted this, so I thought I would post my own solution for anyone else and also get corrections if I've done something wrong.

I had to make an automatic config file parser and I preferred to make numbers int if possible and float if not. The usual try/except conversion doesn't work by itself since any float will just be coerced to an int.

To clarify as someone asked, the case is basically to convert numbers as the person writing the config file data intended them to be. So anything with decimals would probably be intended to be a float. Also, I believe that float can be dangerous for some operators (e.g. ==, <, >) due to the nature of floating point numbers, and int will be converted to float when necessary. Therefore I prefer numbers to stay in int when possible. Not a big thing, just as a kind of convention for myself.


def int_or_float(s):
    try:
        return int(s)
    except ValueError:
        return float(s)

If you want something like "10.0000" converted to int, try this:

def int_dammit_else_float(s):
    f = float(s)
    i = int(f)
    return i if i == f else f

What result do you want for input like "1e25"?


This is one time where it's probably better to get permission first than ask for forgiveness later which is usually considered the 'Pythonic' way to do things and usually involves try/except. So something simple like this might be good enough:

v = float(s) if '.' in s or 'e' in s.lower() else int(s) 

Since there's more than one character that can signal a floating point number, you could also check for one of them these following other ways:

import re
pattern = re.compile(r'[.eE]')
v = float(s) if pattern.findall(s) else int(s)

or

chars = set('.eE')
v = float(s) if any((c in chars) for c in s) else int(s)

Lastly, on a slightly different track, if you wanted floating point whole numbers to become integers, you could do the following, which is similar to @John Machin's "_dammit_" function:

v = int(float(s)) if int(float(s)) == float(s) else float(s)