This article or section documents the current version of Scratch (version 3.0). For this article in Scratch 1.4, see Casting (1.4).

Casting is the conversion of data types for compatibility within methods. For example, a list is cast to a string whenever it is used as a string input. For example, joining a list ["apple", "banana", "cantaloupe"] to "... is a list" will return the string "apple banana cantaloupe... is a list". Scratch's three data types—numbers, booleans, and strings—can all be cast to each type, so there are many rules determining how casting transforms values.

Casting to number

Values are cast to numbers when they are used in blocks' numeric inputs, and when they are compared in blocks such as () = ().

The Boolean value true is converted to the number 1, and the Boolean value false is converted to the number 0. A number value is converted to itself, unless it is NaN ("not-a-number") because of a mathematical error like dividing zero by zero. NaN is converted to 0.

When a String value is cast to a number, Scratch removes spaces at its start and end, then tries to read it as a number and give that interpreted numeric value. The special strings "Infinity" and "+Infinity" are converted to the number Infinity, and the string "-Infinity" is converted to the number -Infinity. Strings that start with "0b", "0o", or "0x" and can be interpreted as binary, octal, or hexadecimal whole numbers are interpreted so. If a string is a plus sign, a minus sign, or neither, then a decimal number, it is converted to that positive, negative, or positive number. A string that is a decimal number then the letter "e" followed by a whole number is converted to the product of the first number with 10 to the power of the second number. Finally, a string that cannot be read as a number any of these ways is converted to 0.

Examples of casting to number
Input value Number
true 1
false 0
123 123
NaN 0
"+Infinity" Infinity
" 0b1010 " 1*8 + 0*4 + 1*2 + 0*1 = 8 + 0 + 2 + 0 = 10
"0o173" 1*64 + 7*8 + 3*1 = 64 + 56 + 3 = 123
"0X7B" 7*16 + 11*1 = 112 + 11 = 123
" +12.5" 12.5
".123e3" 0.123 * 1000 = 123
"-3679e-4 " -3679 * 0.0001 = -0.3679
"cat" 0

Casting to Boolean

Values are cast to Boolean when they are used in a Boolean input. Mostly, only Boolean blocks are allowed in Boolean inputs, but the Item () of () and Item # of () in () reporter blocks are also allowed in Boolean inputs, so any type of value can be cast to Boolean.

Boolean values are converted to themselves. If a string is equal (case-insensitively) to "0", "false", or the empty string "" then it is converted to the Boolean value false. If a number equals zero, then it is converted to the Boolean value false. All other strings and numbers are converted to the Boolean value true.

Examples of casting to Boolean
Input value Boolean
true true
false false
0 false
-12 true
"" false
"0" false
"0.0" true
"fAlSE" false
"cat" true

Casting to string

Values are cast to strings when they are used in string inputs or displayed in Stage monitors.

A string is converted to itself. The Boolean values true and false are converted to the strings "true" and "false". If a numeric value is NaN, it is converted to the string "NaN", or if it equals zero, it is converted to the string "0". Negative numbers are converted to strings beginning with "-", and the numeric value Infinity is converted to the string "Infinity". Numbers with an absolute value greater than or equal to 10-6 and less than 1021 are converted to their decimal representation. Other finite nonzero numbers are represented by a decimal number between 1 and 10, then the lowercase letter "e", then a whole number with a sign so that they are equal to the first number times 10 to the power of the second number. Numbers are rounded to have approximately 16 significant digits.

Examples of casting to string
Input value String
true "true"
false "false"
NaN "NaN"
0 "0"
-0 "0"
Infinity "Infinity"
-0.00000074 "-7.4e-7"
-0.01 "-0.01"
82,589,933 "82589933"
2,176,782,336,000,000,000,000 "2.176782336e+21"
"cat" "cat"

Summary

This table summarizes common rules of casting in Scratch:

Casting Rules String input Number input
Strings String:
"abc"
"abc" 0
String:
"123"
"123" 123
Numbers Number:
123
"123" 123
Booleans Boolean:
true
"true" 1
Boolean:
false
"false" 0
Lists List:
["a", "b", "c"]
"abc" 0
List:
["a", "b", "c", "are the letters"]
"a b c are the letters" 0
List:
["1", "2", "3"]
"123" 123
List:
[1, 2, 3]
"1 2 3" 0
List:
[1, 22, 3]
"1 22 3" 0
List:
["3", ".", "1", "4"]
"3.14" 3.14
List:
[3, ".", 1, 4]
"3 . 1 4" 0
List:
[]
"" 0
Cookies help us deliver our services. By using our services, you agree to our use of cookies.