So if I have a Text Object with the "characters" value: (5 + 2) / 2 How can I set the numeric value of another text object (or any variable of Type Number) to the "evaluated" value?
Ie: how can I set the value to the equivalent of <CMD-P>? Also how can I specify the format as either "7/2" or 3.5?
Thanks, Stephen
Hi Steve,
From a script of the text object you can write:
Compiler evaluate: self getCharacters.
and if the text contains a valid smalltalk expression it will work, otherwise it will throw an error. I don't remember exactly how to catch syntax errors, though. I did it for the spreadsheet object last year, but I wasn't sure if that's the right way.
As for the format, I have no idea. Sorry.
Cheers, Richo
On Wed, Mar 30, 2011 at 6:06 PM, Steve Thomas sthomas1@gosargon.com wrote:
So if I have a Text Object with the "characters" value: (5 + 2) / 2 How can I set the numeric value of another text object (or any variable of Type Number) to the "evaluated" value?
Ie: how can I set the value to the equivalent of <CMD-P>? Also how can I specify the format as either "7/2" or 3.5?
Thanks, Stephen
squeakland mailing list squeakland@squeakland.org http://lists.squeakland.org/mailman/listinfo/squeakland
On 30.03.2011, at 23:28, Ricardo Moran wrote:
Hi Steve,
From a script of the text object you can write:
Compiler evaluate: self getCharacters.
and if the text contains a valid smalltalk expression it will work, otherwise it will throw an error. I don't remember exactly how to catch syntax errors, though. I did it for the spreadsheet object last year, but I wasn't sure if that's the right way.
Here's a textual script to do this with error handling. It needs a "result" variable of type Number, and a "valid" variable of type Boolean.
eval [self setResult: (Compiler evaluate: self getCharacters) asNumber. self setValid: true] on: SyntaxErrorNotification , Error do: [self setValid: false]
See attached project.
As for the format, I have no idea. Sorry.
Use asInteger, asFloat, or asFraction. But note that Etoys always displays numbers as floats. If you want to display a number as fraction, you would need to use something like
self setCharacters: 0.75 asFraction asString
E.g.
Float pi = 3.141592653589793 Float pi asInteger = 3 Float pi asFraction = (884279719003555/281474976710656)
- Bert -
On Thursday 31 Mar 2011 2:36:43 am Steve Thomas wrote:
So if I have a Text Object with the "characters" value: (5 + 2) / 2 How can I set the numeric value of another text object (or any variable of Type Number) to the "evaluated" value
Use Text | numericValue in basic category. The corresponding code equivalents for setting and getting numeric equivalents are: self setNumericValue: 3 and self getNumericValue
HTH .. Subbu
squeakland@lists.squeakfoundation.org