Head-Fi.org › Forums › Misc.-Category Forums › Members' Lounge (General Discussion) › Type casting in python
New Posts  All Forums:Forum Nav:

Type casting in python

post #1 of 7
Thread Starter 
hey, I know there's at least a few software developers here, so I thought i'd ask.
How can you type cast in Python?
mainly, casting a string to a float.
and how would you deal with any fractionals?
i.e. given a = '5.5'
just doing int(a) doesn't exactly work well
Thanks!
post #2 of 7
I came here for an argument, but i guess i have the wrong room.
post #3 of 7
Quote:
Originally Posted by romesaz
hey, I know there's at least a few software developers here, so I thought i'd ask.
How can you type cast in Python?
mainly, casting a string to a float.
and how would you deal with any fractionals?
i.e. given a = '5.5'
just doing int(a) doesn't exactly work well
Thanks!
Unfortunately I don't program in python but I do in alot of other languages.

Usually to typecast something (at least in C style) would be as follows literally.
(int) variable;
(float) variable;

The problem you are facing here is trying to put a string into a data type. I know in java there's string classes to change stings to a data type.
ie.
Code:
double tempPrice = Double.parseDouble(priceString);
With strings to data types it's tricky because you might end up trying to squeeze a 64bit(maybe a double) number to a 32bit data type.
If you are dealing with fractionals, I would use long or double data type.

Sorry I don't know python but at least I might've helped with the logic to it lol
post #4 of 7
Thread Starter 
Thanks for the replies, I actually managed to get it to work
Python is similar to Java, well, rather, the newest 'release' (???) of java is similar to python, so you wouldnt have a problem picking it up, and yea, it was similar to type casting in other languages.
I was just unsure how to go about with floats.
It turns out something as simple as
float('5.5') would actually return a float of 5.5, now my problem is just a huge amount of trailing 0s.
for example,
'12345.54' gets cast to 12345.5400000000001 or somethin like that...*shrug*
thanks anyways!
post #5 of 7
Yeah, data types are kind of weird because every language does things differently. I was having a hard time last week playing around with Perl and C because of how they treat strings and chars. In C, a string is just an array or chars (or actually a pointer to an array of chars), and a character is really just an int. In Perl, a string is a string and you have to do special things if you want to operate on each character you have to use split and then if you want the chars to be ints you have to cast them that way and it's really odd.

Typically turning a string into a number is tricky because what value does a string have? Some aggregate value? The values of the parts? If it's allowed, there's usually some function to parse a string into a number and the trick is finding it and figuring out how to use it. In C I think it's aton() or strtod(). Python is higher level than C though, so I'm not real surprised it takes care of things for you. There's probably an easy way to specify formatting so it doesn't pad your float out with 0s - for sure when you print it, since you probably don't care about extra precision internally
post #6 of 7
Type casting is probablly the wrong terminology your using here... Typecasting in most languages implies conversions to equal or greater percision, though going to going to lesser percision must be an exception.

The problem is that primitives like floats are known to be a certain size (64 bits is it? ) and a string isn't bound by size constraints. The type casting mechanism is a compiler directive which is depending on this sort of agreement or synchornization in data type size in order to not have a problem where it ran out of space when trying to do the conversion, the type your going to didnt have enough bits etc. Thats why there are functions which would handle this, and trunacate, or take away percision without losing bits and producing an error (or meaningless junk)


The issue is in wether python treats strings as objects or not, though i believe it does.
If it is an object, it has helper methods which can do a "conversion" which would be not a typecaste just a representation in a radix you want. How meaningful is this ?


If your working in C or something low level like assembly language, you could literally take the bit "string" as it sits in memory. This would of course require a known length and copying the value out of memory in bits... Then depending on how much bits are being used, you'd have to allow only bit "strings" of less then 64 bits (or whatever the amount of bits floats take) to not lose percision. Then you can make the pointer to the begining of the bit stream a float pointer. I guess that's one way you can do this.
post #7 of 7
Data Type Conversions

int(string) -> int
float(string) -> float
str(int) -> str
str(unicode_str)->str
list(imatable_tuple)->list

And the reason you're getting those weird numbers:

http://www.network-theory.co.uk/docs/pytut/tut_86.html
New Posts  All Forums:Forum Nav:
  Return Home
Head-Fi.org › Forums › Misc.-Category Forums › Members' Lounge (General Discussion) › Type casting in python