What's the squeak code to round a number?
Is it something like this?:
round t1.
Offline
I think t1 rounded, lemme check.
[edit]YEP![/edit]
Last edited by Pecola1 (2011-06-06 10:37:10)
Offline
Thanks people!
Offline
Hmm, I can't get this Panther block to work. Here's my code:
round $Number$ to nearest $Number$
r
| t3 | t3 _ t1 / t2 rounded. abs t3 * t2 - t1 < abs t3 * t2 + 1 - t1 ifTrue: [^ true]. ^ false
What's wrong?
Offline
scimonster wrote:
Hmm, I can't get this Panther block to work. Here's my code:
round $Number$ to nearest $Number$
rCode:
| t3 | t3 _ t1 / t2 rounded. abs t3 * t2 - t1 < abs t3 * t2 + 1 - t1 ifTrue: [^ true]. ^ falseWhat's wrong?
Do I need brackets or something between them, to make sure it evaluates it like this? :
(abs ( ( t3 * t2) - t1) ) < (abs ( ( t3 * ( t2 + 1) ) - t1) )
Offline
scimonster wrote:
scimonster wrote:
Hmm, I can't get this Panther block to work. Here's my code:
round $Number$ to nearest $Number$
rCode:
| t3 | t3 _ t1 / t2 rounded. abs t3 * t2 - t1 < abs t3 * t2 + 1 - t1 ifTrue: [^ true]. ^ falseWhat's wrong?
Do I need brackets or something between them, to make sure it evaluates it like this? :
(abs ( ( t3 * t2) - t1) ) < (abs ( ( t3 * ( t2 + 1) ) - t1) )
I think needs to go after the equation.
Also, there's a shortcut for returning stuff like that. Instead of this:
( ( (t3 * t2) - t1) abs ) < ( ( (t3 * t2) + 1) abs - t1) ifTrue: [^ true].
^ false
Use this:
^ ( ( (t3 * t2) - t1) abs ) < ( ( (t3 * t2) + 1) abs - t1)
Offline
scimonster wrote:
scimonster wrote:
Hmm, I can't get this Panther block to work. Here's my code:
round $Number$ to nearest $Number$
rCode:
| t3 | t3 _ t1 / t2 rounded. abs t3 * t2 - t1 < abs t3 * t2 + 1 - t1 ifTrue: [^ true]. ^ falseWhat's wrong?
Do I need brackets or something between them, to make sure it evaluates it like this? :
(abs ( ( t3 * t2) - t1) ) < (abs ( ( t3 * ( t2 + 1) ) - t1) )
Put the brackets, if it takes them away automatically you don't need them.
Offline
I currently have this as the code:
round $Number$ to nearest $Number$
-Code:
| t3 | t3 _ t1 / t2 rounded. ( ( (t3 * t2) - t1) abs ) < ( ( (t3 * t2) + 1) - t1) abs ifTrue: [^ ( ( (t3 * t2) - t1) abs ) rounded]. ^ ( ( (t3 * t2) + 1) - t1) abs rounded
It always reports 0 though!
Offline
round $Number to nearest $Number$
^ ((t1 / t2) rounded) * t2
Does this work?
Offline
TheSuccessor wrote:
round $Number to nearest $Number$
^ ((t1 / t2) rounded) * t2
Does this work?
Yes, thank you!
How was that so simple?
Offline