s LearnSqueak.com

ABSOLUTE BEGINNER

Your first rounding

Turn a decimal into a nearby whole number, then compare rounding down and rounding up in five tiny Squeak steps.

5 minutes · 5 small steps · Squeak 6.1

STEP 1

Round a number

Open a Workspace from Squeak's World menu. Type this line, select all of it, then choose Print it from the text menu.

Select the whole expression, then Print it
3.7 rounded
NATIVE SQUEAK CAPTURE
Native Squeak rounding checkpoint 1
Actual native Squeak Workspace Print it result.
You should see

You should see 4. Remove the selected answer before trying another line.

STEP 2

Try a smaller decimal

rounded chooses a nearest whole number. Here the nearest whole number is 3.

Select the whole expression, then Print it
3.2 rounded
NATIVE SQUEAK CAPTURE
Native Squeak rounding checkpoint 2
Actual native Squeak Workspace Print it result.
You should see

You should see 3.

STEP 3

Go down

floor chooses the whole number at or below your number.

Select the whole expression, then Print it
3.7 floor
NATIVE SQUEAK CAPTURE
Native Squeak rounding checkpoint 3
Actual native Squeak Workspace Print it result.
You should see

You should see 3, even though 3.7 is closer to 4.

STEP 4

Go up

ceiling chooses the whole number at or above your number.

Select the whole expression, then Print it
3.2 ceiling
NATIVE SQUEAK CAPTURE
Native Squeak rounding checkpoint 4
Actual native Squeak Workspace Print it result.
You should see

You should see 4.

STEP 5

Try a negative number

Down means further left on the number line. Write the negative number inside parentheses.

Select the whole expression, then Print it
(-3.2) floor
NATIVE SQUEAK CAPTURE
Native Squeak rounding checkpoint 5
Actual native Squeak Workspace Print it result.
You should see

You should see -4. Try (-3.2) ceiling too: it gives -3.

YOU DID IT

A small step. A real result.

rounded chooses a nearby whole number. floor goes down; ceiling goes up. Try these messages on both positive and negative numbers.

Optional code download: open it as text and copy one example into Workspace.

Download the five examples; copy one at a time

Your first list · More Smalltalk messages