---
title: "to_i8"
url: https://una-lang.org/en/docs/std/builtin/to_i8/
release: v0.4.0
edition: "0.2 and later"
source: https://github.com/una-lang/una/blob/v0.4.0/docs/std/builtins.json
---

# to_i8

A built-in method, resolved by the type of its receiver ([`spec/14 §2`](https://una-lang.org/en/docs/spec/14-standard-library/#s2)). Edition 0.2 and later.

```una
x.to_i8() -> I8
  where x: Int
x.to_i8() -> I8
  where x: F, one of I8, I16, I32, I64, U8, U16, U32, U64
```

Converts `x` to that type. A widening conversion, where every value of the source type fits the target, always succeeds; a narrowing one carries an `overflow` obligation that the value fits, or goes through `checked`, which counts against the escape budget (`spec/18 §4`).

## Receiver

- `Int`: an integer `x`
- `F, one of I8, I16, I32, I64, U8, U16, U32, U64`: a fixed\-width integer `x`\, of one of the eight types `I8`\, `I16`\, `I32`\, `I64`\, `U8`\, `U16`\, `U32` and `U64`

## Returns

`I8`: the value of `x` as an `I8`\: from `I8` the conversion widens\, so it always succeeds\; from `Int` and every other type it narrows\, and a narrowing conversion carries an `overflow` obligation that the value fits\, which `una check` proves at the call\, or goes through `checked`\, which counts against the escape budget.

## See also

[`to_int`](https://una-lang.org/en/docs/std/builtin/to_int/)

## Example

`src/shop.una`

```una
module shop

--- A small stock adjustment kept in one byte, where it fits.
adjustment(change: Int) -> I8 = if change >= -128 and change <= 127 then change.to_i8() else 0

pub main() -> Unit
  uses @console: Console[print]
=
  let returned = adjustment(-5)
  let unknown = adjustment(500)
  @console.print("{returned}")
  @console.print("{unknown}")
```

`una run .` prints:

```text
-5
0
```

## Specification

- [`spec/18 §4`](https://una-lang.org/en/docs/spec/18-fixed-width-integers/#s4)

Generated from una v0.4.0 ([`docs/std/builtins.json`](https://github.com/una-lang/una/blob/5fec93d88932b333feec25e589355a2cb34c37b5/docs/std/builtins.json), which is not part of the specification): built-in methods have no declaration, so una writes these signature lines and tests each against its checker. The specification governs: [`spec/14 §2`](https://una-lang.org/en/docs/spec/14-standard-library/#s2).
