Type casting in python
Nov 20, 2005 at 3:05 AM Thread Starter Post #1 of 7

romesaz

100+ Head-Fier
Joined
Oct 31, 2004
Posts
308
Likes
10
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
biggrin.gif

Thanks!
 
Nov 20, 2005 at 5:16 AM 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
biggrin.gif

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:

Code:
[left]double tempPrice = Double.parseDouble(priceString);[/left]

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
 
Nov 20, 2005 at 5:56 AM Post #4 of 7
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!
 
Nov 20, 2005 at 5:42 PM 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
 
Nov 20, 2005 at 6:03 PM 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.
 
Nov 20, 2005 at 11:05 PM Post #7 of 7

Users who are viewing this thread

Back
Top