[ENH] String and Character conversion methods
Cancerbero
sgurin at montevideo.com.uy
Thu Jul 29 04:49:27 UTC 2004
Hi. I don't know how formally make this kind of suggestions so excuse
me if I'm doing it wrong.
My situation is this: The user's input is a String representing a
floating point number, so I want something that converts a String
representing a floating point number to a Float. Example: Something like
String>>asFloat so '3.14' asFloat returns the Float 3.14.
So my suggesiont is to add a method String>>asFloat. For example:
String>>asFloat
"returns the float number represented by this String. For example
'0.01' asFloat will return the Float 0.1e-1. The strings MUST be
representing float numbers in the format 9.12345 and not in the
format 0.1e-4. TO DO: the conversion when the represented float is in
the format: 0.1e-4. "
|x exp point|
x:=0.0.
point:=false.
(self size) to: 1 by: -1 do: [: i|
((self at: i)=$.)
ifTrue: [point:=true]
ifFalse: [
point
ifTrue: [exp:=self size - i - 1]
ifFalse: [exp:=self size - i].
x:=x+(((self at: i)asDigit)*(10 raisedTo: exp)).
].
].
(self size) to: 1 by: -1 do: [: i|
((self at: i)=$.) ifTrue: [x:=x * (10 raisedTo: (i-(self size)))]
].
^x
where Character>>asDigit is something like:
Character>>asDigit
"Example : $4 asDigit will return the SmallInteger 4"
^(self asInteger)-($0 asInteger)
Bye
--
Sebastián Gurin (Cancerbero)
cancerbero_sgxARROBAusers.sourceforge.net
http://www.geocities.com/cancerbero_sgx
Un poco de sabiduría sí es posible; mas en todas las cosas he
hallado esta feliz certidumbre: prefieren bailar sobre los
pies del azar.
--Nietzsche
More information about the Squeak-dev
mailing list
|