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
On Wednesday 28 July 2004 9:49 pm, Cancerbero wrote:
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:
What about
'3.14' asNumber => 3.14 '3' asNumber => 3
or if you really wanted to be sure:
'3.14' asNumber asFloat => 3.14 '3' asNumber asFloat => 3.0
Cancerbero sgurin@montevideo.com.uy wrote:
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:
That's a nice method to have around, but it should be implemented by using the same code that is already present for reading floats. That code can be used like this:
Float readFromString: '3.14'
The thing is, it would be nice if asFloat does *exactly* the same thing as "Float readFromString:", and if future tweaks to either one also take effect on the other.
-Lex
Hi Cancerbero,
It seems that Cancerbero wrote:
So my suggesiont is to add a method String>>asFloat.
There are already some conversion methods you can use.
For example, Float readFrom: '3.14'. does what you want (it uses the Compiler to evaluate the text).
Perhaps you will want to anyway define String>>asFloat as
String>>asFloat ^ Float readFrom: self.
Character>>asDigit "Example : $4 asDigit will return the SmallInteger 4"
$4 digitValue does that conversion.
Bye!
German Morales (aka Yoda)
squeak-dev@lists.squeakfoundation.org