---
title: "to_int"
url: https://una-lang.org/en/docs/std/prelude/to_int/
release: v0.4.0
edition: "0.1 and later"
source: https://github.com/una-lang/una/blob/v0.4.0/docs/std/listing.json
---

# to_int

A function of the prelude, in scope in every module. Edition 0.1 and later.

```una
pub to_int(t: Text) -> Option[Int]
```

`Some` of the integer that `t` writes when `t` is an optional `-` followed by one or more ASCII digits, leading zeros allowed, and `None` for any other text: the empty text, `"-"`, a leading `+`, whitespace or digits outside ASCII. `Int` has no bound, so a long text of digits is never `None` for its length (`spec/14 §1`).

## Parameters

- `t`: the text

## Returns

`Option[Int]`: `Some` of the integer `t` writes\, an optional `-` and from 1 to 4300 ASCII digits\; `None` for any other text\, among them a leading `+` and more than 4300 digits.

## Use instead

- [`parse_int`](https://una-lang.org/en/docs/std/prelude/parse_int/) (edition 0.2 on) to know why a text is not a number

## See also

[`is_digits`](https://una-lang.org/en/docs/std/prelude/is_digits/), [`trim`](https://una-lang.org/en/docs/std/prelude/trim/)

## Example

`src/shop.una`

```una
module shop

--- A stock adjustment typed by hand: a sign is allowed, so `-3` writes off three units.
adjustment(raw: Text) -> Text
=
  when to_int(raw)
    is Some(n) -> "'{raw}': adjust the stock by {n}"
    is None -> "'{raw}': not an adjustment"

pub main() -> Unit
  uses @console: Console[print]
=
  @console.print(adjustment("12"))
  @console.print(adjustment("-3"))
  @console.print(adjustment("+3"))
```

`una run .` prints:

```text
'12': adjust the stock by 12
'-3': adjust the stock by -3
'+3': not an adjustment
```

## Specification

- [`spec/14 §1`](https://una-lang.org/en/docs/spec/14-standard-library/#s1)

Generated from the standard modules of una v0.4.0 ([`docs/std/listing.json`](https://github.com/una-lang/una/blob/5fec93d88932b333feec25e589355a2cb34c37b5/docs/std/listing.json), which is not part of the specification). The specification governs: [`spec/14 §1`](https://una-lang.org/en/docs/spec/14-standard-library/#s1).
