> ## Documentation Index
> Fetch the complete documentation index at: https://gocharting.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Values and types

> The five basic types and their literals, na, typed declarations, conversions, reassignment and the whole-number rule for division.

The basic types are `int`, `float`, `bool`, `string` and `color`. Arrays, drawings (`label`, `line`, ...) and your own types and enums
are covered in [Arrays](https://gocharting.com/docs/scripting/language/arrays.md) and [Custom types and enums](https://gocharting.com/docs/scripting/language/custom-types.md).

## Literals

| Type | Written as |
| - | - |
| `int` | `42` (no hexadecimal, no `1_000`). |
| `float` | `3.14`, `.5`, `2.`, `1e3`, `1.5e-2`. |
| `bool` | `true`, `false`. |
| `string` | `"text"` or `'text'`, on one line. Put a quote inside by using the other kind: `"say 'hi'"`. `+` joins two strings. |
| `color` | `#RRGGBB` or `#RRGGBBAA` (the last byte is the alpha, `FF` opaque), or a constant such as `color.red`. |

`color.new(color.red, 0.5)` sets an opacity from 0 (invisible) to 1 (solid); a value outside that range is clamped, with a warning.

## na

`na` means "no value": the first bars of a moving average, or history that does not reach back far enough. Arithmetic with an `na`
operand gives `na`, and so does a division by zero.

* Test with `na(x)` (not `x == na`, which warns); replace with `nz(x)` (0) or `nz(x, 5)`.
* A variable needs a type to start as `na`: `float x = na`. Without one: Value with NA type cannot be assigned to a variable that was
  defined without type keyword.

## Declaring and typing a variable

`name = value` declares a variable, and its type is the type of the first value: `x = 0` is an `int`. Write the type to choose another:
`float x = 0`. A typed declaration takes a compatible value: `float` takes an `int`, `bool` takes a number, `int` takes neither a
`float` nor a `bool`.

`:=` gives an existing variable a new value of its type; `+=`, `-=`, `*=`, `/=`, `%=` do the same with the result of the operation
(`+=` also joins strings).

```lipi-invalid theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Wrong type")
x = 0
x := close
plot(x)
```

The compiler says: Variable `x` was declared with `const int` type. Declared type `int` is not compatible with assigned type `series float`. Use math.floor(), math.round(), or math.ceil() if you need a whole number.
Declare `float x = 0` instead.

## Whole numbers

`/` always gives a `float`, even for two integers, so `int half = length / 2` is refused with the message above (Declared type `int` is not compatible with
assigned type `input float`). Write `int half = math.floor(length / 2)`; those functions return an `int`. `/=` is refused on an `int` variable for the same reason.

## Converting

* `int(x)` drops the fraction toward zero (`int(-2.9)` is `-2`) and gives a typed `na` for `na`.
* `bool(x)` turns a number into `false` when it is zero and `true` otherwise; most bool arguments take a number the same way
  (`alertcondition` does not). `float(x)` turns an int into a float. `string(5)` and `int("5")` are refused.
* Number to text: `str.tostring(x)`. Text to number: `str.tonumber(s)`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.