user2921139
user2921139

Reputation: 1789

type of unicode never changes even after typecasting python

Does anyone know why the type of a unicode string never changes even after typecasting. This is in Python and I have a sample that I tried out -similar to what is going on in my script.

I need to check if 2 variables (ints) should be equal. One of them is an int but the other one, even after using int/float types the type never changes from unicode.

>>> x = 999.99
>>> type(x)
<type 'float'>
>>> x = u'999.99'
>>> type(x)
<type 'unicode'>
>>> x.decode('utf-8')
u'999.99'
>>> type(x)
<type 'unicode'>
>>> float(x)
999.99
>>> type(x)
<type 'unicode'>
>>> int(float(x))
999
>>> type(x)
<type 'unicode'>
>>> 

Upvotes: 0

Views: 31

Answers (1)

ruakh
ruakh

Reputation: 183466

float(x) doesn't actually modify x, it just returns a float-casted copy of it. I guess what you want is:

x = int(float(x))

Upvotes: 2

Related Questions