78

I have a string in the format: 'nn.nnnnn' in Python, and I'd like to convert it to an integer.

Direct conversion fails:

>>> s = '23.45678'
>>> i = int(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '23.45678'

I can convert it to a decimal by using:

>>> from decimal import *
>>> d = Decimal(s)
>>> print d
23.45678

I could also split on '.', then subtract the decimal from zero, then add that to the whole number ... yuck.

But I'd prefer to have it as an int, without unnecessary type conversions or maneuvering.

Matt Hampel
  • 5,088
  • 12
  • 52
  • 78
  • 14
    Matt, 23.45678 isn't an integer. If you want Python to convert a string to an integer, make sure that you give it an integer. Arguments can be made either way about what the "right" thing is, and in the end someone is going to be unhappy. – Christopher Jul 07 '09 at 20:49

8 Answers8

145

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.

Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
  • 6
    Remember to `import decimal` then use it as `decimal.Decimal(5)` or do `from decimal import Decimal` and use it as `Decimal(5)`. I always forget. – teewuane Apr 28 '15 at 00:42
  • You should note the type of rounding each option offers to the value. – Rapnar Dec 11 '15 at 14:30
  • Note that the `s.split('.')`-option will not reject and explain bad input-strings as well as the others do. You might get poor error-messages in the best case and questionable results in the worst case. – julaine Jul 04 '23 at 09:42
16

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))
Dan Lorenc
  • 5,376
  • 1
  • 23
  • 34
4
>>> 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...

Kevin Little
  • 12,436
  • 5
  • 39
  • 47
3

You could use:

s = '23.245678'
i = int(float(s))
Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
gbc
  • 8,455
  • 6
  • 35
  • 30
2

"Convert" only makes sense when you change from one data type to another without loss of fidelity. The number represented by the string is a float and will lose precision upon being forced into an int.

You want to round instead, probably (I hope that the numbers don't represent currency because then rounding gets a whole lot more complicated).

round(float('23.45678'))
JosefAssad
  • 4,018
  • 28
  • 37
1

The expression int(float(s)) mentioned by others is the best if you want to truncate the value. If you want rounding, using int(round(float(s)) if the round algorithm matches what you want (see the round documentation), otherwise you should use Decimal and one if its rounding algorithms.

Kathy Van Stone
  • 25,531
  • 3
  • 32
  • 40
0
round(float("123.789"))

will give you an integer value, but a float type. With Python's duck typing, however, the actual type is usually not very relevant. This will also round the value, which you might not want. Replace 'round' with 'int' and you'll have it just truncated and an actual int. Like this:

int(float("123.789"))

But, again, actual 'type' is usually not that important.

rledley
  • 2,325
  • 1
  • 12
  • 7
0

I believe this is a useless bug that should be corrected in Python.

int('2') --> 2 That converts the string '2' into an the integer 2.

int(2.7) --> 2 Converts a float to an int.

int('2.7') SHOULD convert to 2. This is how Perl works, for example. Yes, this does two things at once. It converts the string and when it finds it is in a representation that is a float, it should convert to int.

Otherwise, why insist that float('2') should work? It is an integer string, because there is no decimal point. So it has to convert from string which is an integer, directly to float.

I don't know but perhaps someone can answer whether the python interpreter, if the required int(float(x)) is used, if it actually goes through the process of first converting to float and then converting to int. That would make this bug even more critical to correct.

Ray Lutz
  • 91
  • 5