Convert a string to integer with decimal in Python

Question:

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.

Asked By: Matt Hampel

||

Answers:

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.

Answered By: Paolo Bergantino

You could use:

s = '23.245678'
i = int(float(s))
Answered By: gbc

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))
Answered By: Dan Lorenc

“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'))
Answered By: JosefAssad
>>> 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…

Answered By: Kevin Little
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.

Answered By: rledley

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.

Answered By: Kathy Van Stone

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.

Answered By: Ray Lutz
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.