| 1 |
--- |
| 2 |
title: cast.ToInt |
| 3 |
description: Converts a value to a decimal integer (base 10). |
| 4 |
keywords: [] |
| 5 |
params: |
| 6 |
functions_and_methods: |
| 7 |
aliases: [int] |
| 8 |
returnType: int |
| 9 |
signatures: [cast.ToInt INPUT] |
| 10 |
aliases: [/functions/int] |
| 11 |
--- |
| 12 |
|
| 13 |
With a decimal (base 10) input: |
| 14 |
|
| 15 |
```go-html-template |
| 16 |
{{ int 11 }} → 11 (int) |
| 17 |
{{ int "11" }} → 11 (int) |
| 18 |
|
| 19 |
{{ int 11.1 }} → 11 (int) |
| 20 |
{{ int 11.9 }} → 11 (int) |
| 21 |
``` |
| 22 |
|
| 23 |
With a binary (base 2) input: |
| 24 |
|
| 25 |
```go-html-template |
| 26 |
{{ int 0b11 }} → 3 (int) |
| 27 |
{{ int "0b11" }} → 3 (int) |
| 28 |
``` |
| 29 |
|
| 30 |
With an octal (base 8) input (use either notation): |
| 31 |
|
| 32 |
```go-html-template |
| 33 |
{{ int 011 }} → 9 (int) |
| 34 |
{{ int "011" }} → 9 (int) |
| 35 |
|
| 36 |
{{ int 0o11 }} → 9 (int) |
| 37 |
{{ int "0o11" }} → 9 (int) |
| 38 |
``` |
| 39 |
|
| 40 |
With a hexadecimal (base 16) input: |
| 41 |
|
| 42 |
```go-html-template |
| 43 |
{{ int 0x11 }} → 17 (int) |
| 44 |
{{ int "0x11" }} → 17 (int) |
| 45 |
``` |
| 46 |
|
| 47 |
> [!NOTE] |
| 48 |
> Values with a leading zero are octal (base 8). When casting a string representation of a decimal (base 10) number, remove leading zeros: |
| 49 |
|
| 50 |
`{{ strings.TrimLeft "0" "0011" | int }} → 11` |