- 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.
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 |
Decimal numbers | Infinity | Non-decimal whole numbers | ||
---|---|---|---|---|
1 or more decimal digits (0–9), possibly with 1 decimal point (.) before, after, or between them | ||||
Any above optionally followed by e or E, then +, -, or neither, then 1 or more decimal digits (0–9) | "Infinity" (case-sensitive) | |||
Any above prefixed with +, -, or neither | 0b or 0B, then 1 or more binary digits (0–1) | 0o or 0O, then 1 or more octal digits (0–7) | 0x or 0X, then 1 or more hexadecimal digits (0–9, a–f, A–F) | |
Any above with 0 or more spaces before and after |
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.
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.
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" |
Comparison
Some blocks that need to compare values of all types use a special casting routine that takes two inputs, might convert them to number or string, and decides either that one input is greater than the other or that the inputs are equal. These blocks include () < (), () = (), () > (), Item # of () in (), and () Contains ()?.
The first step of comparison is to decide whether the inputs are numbers. Boolean values are numbers, numeric values are numbers unless they are NaN, and string values are numbers if they represent a number by the rules of casting string to number.
If both inputs are numbers, they are cast to number and compared in numerical order. Infinity equals Infinity and is greater than all other numbers; -Infinity equals -Infinity and is less than all other numbers. Finite numbers are ordered by subtracting them and checking whether their difference is zero, positive, or negative.
If at least one input is not a number, both inputs are cast to string, converted to lowercase, and compared in alphabetical order. Comparing the lowercase version of the strings finds the first position number where the two have different characters, and chooses as the greater string the one whose character is encoded as a higher number. If there is no position where the strings have two different characters, the longer string is greater; if the strings have the same length and all the same characters, they are equal.
Input value | Is a number? | Number | Lowercase string |
---|---|---|---|
true | yes | 1 | "true" |
1 | yes | 1 | "1" |
Infinity | yes | Infinity | "infinity" |
NaN | no | "nan" | |
"" | no | "" | |
"true" | no | "true" | |
"1. " | yes | 1 | "1. " |
"+Infinity" | yes | Infinity | "+infinity" |
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 |