Convert a string to integer with decimal in Python

PythonStringIntegerDecimal

Python Problem Overview


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.

Python Solutions


Solution 1 - 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.

Solution 2 - Python

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))

Solution 3 - Python

>>> 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...

Solution 4 - Python

You could use:

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

Solution 5 - Python

"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'))

Solution 6 - Python

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.

Solution 7 - Python

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.

Solution 8 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatt HampelView Question on Stackoverflow
Solution 1 - PythonPaolo BergantinoView Answer on Stackoverflow
Solution 2 - PythonDan LorencView Answer on Stackoverflow
Solution 3 - PythonKevin LittleView Answer on Stackoverflow
Solution 4 - PythongbcView Answer on Stackoverflow
Solution 5 - PythonJosefAssadView Answer on Stackoverflow
Solution 6 - PythonKathy Van StoneView Answer on Stackoverflow
Solution 7 - PythonrledleyView Answer on Stackoverflow
Solution 8 - PythonRay LutzView Answer on Stackoverflow