This tutorial explains how to check if a given number is an integer, or whole number, like 7 or -3 (as opposed to a fraction like 1.1 or ¾ or an irrational number like the square root of 2).

Method 1

This Boolean expression will report true if (number) is an integer:

<(round (number)) = (number)>

This is because the Round block reports (in most cases) the closest integer to the given number, and if it is already an integer it simply reports its input. The "floor" and "ceiling" options of the () of () block also work.

Method 2

If (number) is an integer, then the following will be true:

<((number) mod (1)) = (0)>

This works because the () Mod () block reports the remainder even if the dividend is not an integer; for example, ((1.5) mod (1)) is 0.5.

Method 3

set [count v] to [0]
repeat ([abs v] of (number):: operators)
    change [count v] by (1)
end
if <(count) = ([abs v] of (number))> then
    ...
end

This works because the Repeat () block rounds its input, so the number of times the blocks inside the mouth run is an integer. (Abs is used because Repeat () cannot run the blocks a negative number of times.)

Cookies help us deliver our services. By using our services, you agree to our use of cookies.