Convert a string to integer with decimal in Python Convert a string to integer with decimal in Python python python

Convert a string to integer with decimal in Python


How about this?

>>> s = '23.45678'>>> int(float(s))23

Or...

>>> int(Decimal(s))23

Or...

>>> int(s.split('.')[0])23

I doubt it's going to get much simpler than that, I'm afraid. Just accept it and move on.


What sort of rounding behavior do you want? Do you 2.67 to turn into 3, or 2. If you want to use rounding, try this:

s = '234.67'i = int(round(float(s)))

Otherwise, just do:

s = '234.67'i = int(float(s))


>>> s = '23.45678'>>> int(float(s))23>>> int(round(float(s)))23>>> s = '23.54678'>>> int(float(s))23>>> int(round(float(s)))24

You don't specify if you want rounding or not...