From Test-Scratch-Wiki

This tutorial explains how to check if a number given is an integer. An integer is a number that, when divided by 1, has no remainder. In other terms, integers cannot be fractions, mixed or irrational.

Method 1

This Boolean expression will return true if the number is an integer:

<([floor v] of (number)) = (number)>

This is because the "floor" function in the () of () block returns the highest integer lower or equal to the number, and if the number is already an integer, the result will be equal to the number. The "ceiling" option and the Round () block also work.

Method 2

If the Variable "t" is an integer, the sprite will say "t is an integer!".

set [t_checkint v] to (t)
repeat (t)
change [t_checkint v] by (-1)
if <(t_checkint) = [0]> then
say [t is an integer!] 

Method 3

If "n" is an integer, then this boolean will be true:

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

This works because the () Mod () (block) block tells you the exact remainder even if it isn't an integer. So

((1.2478) mod (1))

would report 0.2478, not 0.

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