Matt Hampel
Matt Hampel

Reputation: 5237

Convert a string to integer with decimal in Python

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.

Upvotes: 81

Views: 294427

Answers (8)

Ray Lutz
Ray Lutz

Reputation: 79

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.

Upvotes: 0

gbc
gbc

Reputation: 8555

You could use:

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

Upvotes: 3

Kathy Van Stone
Kathy Van Stone

Reputation: 26281

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.

Upvotes: 1

rledley
rledley

Reputation: 2431

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.

Upvotes: 0

Kevin Little
Kevin Little

Reputation: 12976

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

Upvotes: 4

JosefAssad
JosefAssad

Reputation: 4128

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

Upvotes: 2

Dan Lorenc
Dan Lorenc

Reputation: 5394

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

Upvotes: 16

Paolo Bergantino
Paolo Bergantino

Reputation: 488414

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.

Upvotes: 148

Related Questions