pxt-calliope/docs/reference/types/number.md

72 lines
1.8 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Number
2016-04-02 01:22:47 +02:00
An integer number.
2016-03-26 00:47:20 +01:00
### @parent blocks/language
A *Number* is an integer such as `42` or `-42`. More precisely, a *Number* is a signed 32-bit integer (two's complement).
### Declare a number variable
2016-06-15 03:37:55 +02:00
You can assign a number to a variable:
2016-03-26 00:47:20 +01:00
2016-06-15 03:37:55 +02:00
```blocks
let num = 42;
basic.showNumber(42);
```
2016-03-26 00:47:20 +01:00
### Arithmetic operators
2016-04-13 17:27:45 +02:00
The following arithmetic operators work on numbers and return a [Number](/reference/types/number):
2016-03-26 00:47:20 +01:00
* addition: `1 + 3`
* subtraction: `1 - 3 `
* multiplication: `3 * 2`
* integer division: `7 / 3`
2016-06-15 03:37:55 +02:00
* modulo is available through the [math library](/blocks/math)
2016-03-26 00:47:20 +01:00
### Relational operators
2016-06-15 13:55:19 +02:00
The following relational operators work on numbers and return a [Boolean](/blocks/logic/boolean):
2016-03-26 00:47:20 +01:00
* equality: `(3 + 1) = 4`
* inequality: `3 != 4`
* less or equal than: `3 <= 4`
* less than: `3 < 4`
* greater or equal than : `4 >= 3`
* greater than: `4 > 3`
### Show number
2016-06-15 03:37:55 +02:00
The [show number](/reference/basic/show-number) function displays a number on the [LED screen](/device/screen).
For example, this code displays the number 42:
2016-03-26 00:47:20 +01:00
2016-06-15 03:37:55 +02:00
```blocks
basic.showNumber(42);
```
2016-03-26 00:47:20 +01:00
### Functions that return a number
2016-06-15 03:37:55 +02:00
Some functions return a number, which you can store in a variable.
For example the following code gets the display brightness
(using the [brightness function](/reference/led/brightness)) and stores the value in a variable named `brightness`:
2016-03-26 00:47:20 +01:00
2016-06-15 03:37:55 +02:00
```blocks
let brightness = led.brightness()
```
2016-03-26 00:47:20 +01:00
### Math functions
2016-06-19 14:28:46 +02:00
The [math library](/blocks/math) includes math related functions.
2016-06-15 03:37:55 +02:00
For example, the `absolute` function returns the returns the absolute value of input parameter `x`:
2016-03-26 00:47:20 +01:00
2016-06-15 03:37:55 +02:00
```blocks
let abs = Math.abs(-42);
2016-06-15 03:37:55 +02:00
basic.showNumber(abs);
```
2016-03-26 00:47:20 +01:00
### See also
2016-06-15 13:55:19 +02:00
[math](/blocks/math), [var](/blocks/variables/var), [Boolean](/blocks/logic/boolean), [show number](/reference/basic/show-number)
2016-03-26 00:47:20 +01:00