The Squirrel Programming Guide

# Variable Types: Numbers

## Integers

Integers are whole, non-fractional numbers. In Squirrel, integers are 32 bits long and are always signed: they can be positive or negative.

Integers are scalar values: adding an integer to two entities, such as a pair of arrays, will store two independent copies of the integer’s value. Passing an integer into one of a function’s parameters as an argument will likewise create a new copy of the value.

An integer’s four bytes are stored in little endian order: the least significant byte in memory location n and the most significant byte in memory location n+4.

Calling `typeof` on an integer returns the string `"integer"`.

### Characters As Integers

Squirrel supports characters as integer literals. The variable is assigned the Ascii code of the character literal, which must be placed within single quote marks. For example:

``````local someValue = '*';
server.log(someValue);
// Displays '42'
``````

Note You can include only one character between single quote marks; impCentral’s syntax checker will reject code that contains multiple characters between single quotes with the error `constant too long`.

### Integer Delegate Methods

Integers have access to a number of delegate methods to help with value conversion.

• tochar() — Returns the target integer as a single-character string. The integer value should lie between -127 and 128, but Squirrel will accept integers out of this range and simply derive the character value from the lowest eight bits of the passed value.

``````local someValue = 42;
server.log(someValue.tochar());
// Displays '*'
``````
• tofloat() — Returns the target integer value as a float.

``````local someValue = 42;
server.log(someValue.tofloat());
// Displays '42.0'
``````
• tostring() — Returns a string representation of the target integer.

``````local someValue = 42;
server.log(someValue.tostring());
// Displays '42'
``````

## Floats

Floats are fractional numbers. In Squirrel, floats are 32 bits long and can be positive or negative.

Floats are scalar values: adding a float to two entities, such as a pair of arrays, will store two independent copies of the float’s value. Passing a float into a function as an argument will likewise create a new copy of the value.

Calling `typeof` on a float returns the string `"float"`.

### Float Delegate Methods

Floats have access to a number of delegate methods to help with value conversion.

• tochar() — Returns the target float as a single-character string.

``````local someValue = 42.0;
server.log(someValue.tochar());
// Displays '*'
``````
• tointeger() — Returns the target float value as an integer.

``````local someValue = 42.0;
server.log(someValue.tofloat());
// Displays '42'
``````
• tostring() — Returns a string representation of the target float.

``````local someValue = 42.01;
server.log(someValue.tostring());
// Displays '42.01'
``````

## Booleans

Booleans, aka ‘bools’, are single-byte integers representing either of the binary states `true` or `false`, both of which are identified as such in code.

``````local isConnected = true;
local isConnecting = false;
``````

However, bools can be converted to integers and floats, with `true` becoming 1 and `false` 0:

``````local aBool = true;
local anInt = aBool.tointeger();    // 'aInt' equals 1
local aFloat = aBool.tofloat();     // 'aFloat' equals 1.0
``````

Many statements using operators result in the generation of a boolean value. For example, `(x == y)` will evaluate to `true` if the values of x and y are the same, otherwise it will evaluate to `false`. We’ll see in the program control chapter how that result can be used to cause Squirrel to choose one code path among several.

Bools are scalar values: adding a bool to two entities, such as a pair of arrays, will store two independent copies of the bool’s value. Passing a bool into a function as an argument will likewise create a new copy of the value.

Calling `typeof` on a bool returns the string `"bool"`.

### Boolean Delegate Methods

Booleans have access to a number of delegate methods to help with value conversion:

• tofloat() — Returns the target bool as a float. See above for an example.

• tointeger() — Returns the target bool as an integer. See above for an example.

• tostring() — Returns a string representation of the target bool.

``````local aBool = true;
server.log(aBool.tostring());
// Displays 'true'
``````

Back to the top