getNumber

Category:UI Controls

Gets the number from the specified screen element.

To capture numeric data entered by the user your apps will need to read data from textInput() or slider screen elements*. getNumber() is usually used in an onEvent() callback function, and returns a number that can be stored in a variable, used as a parameter in another function call, or used in an arithmetic expression.

*The slider design element was contributed by Mike and Mitchell Schmidt.

Examples

Example: Fahrenheit to Celcius

Demonstrate reading a number from a slider. Assumes a slider named todayTempID has been placed on the screen in design mode.

// Demonstrate reading a number from a slider. Assumes a slider named todayTempID has been placed on the screen in design mode.
var todayTemp=50;
textLabel("id", "Choose a temperature (in F) with the slider:");
textLabel("displayTemp", todayTemp);
onEvent("todayTempID", "change", function() {
  todayTemp=getNumber("todayTempID");
  setText("displayTemp",todayTemp);
});
button("convert", "Convert");
setPosition("convert", 210, 20);
onEvent("convert", "click", function() {
  var celcius=(todayTemp-32)/1.8;
  write(todayTemp+ " Fahrenheit is " + celcius + " Celcius");
});

textInput("id", "Enter your age");
onEvent("id", "change", function(event) {
  write("In 10 more years you will be " + (getNumber("id")+10));
});

Syntax

getNumber(id)

Parameters

Name Type Required? Description
id string The unique identifier for the screen element. Must begin with a letter, contain no spaces, and may contain letters, digits, - and _.

Returns

A number containing the contents of the screen element.

Tips

  • getText() can also read textInput screen elements, but a number will be converted to a string and arithmetic will not be possible with the result.

Found a bug in the documentation? Let us know at documentation@code.org