int.md (1203B)
1 --- 2 title: int 3 linktitle: int 4 description: Creates an `int` from the argument passed into the function. 5 date: 2017-02-01 6 publishdate: 2017-02-01 7 lastmod: 2017-02-01 8 categories: [functions] 9 menu: 10 docs: 11 parent: "functions" 12 keywords: [strings,integers] 13 signature: ["int INPUT"] 14 workson: [] 15 hugoversion: 16 relatedfuncs: [] 17 deprecated: false 18 aliases: [] 19 --- 20 21 Useful for turning strings into numbers. 22 23 ``` 24 {{ int "123" }} → 123 25 ``` 26 27 {{% note "Usage Note" %}} 28 If the input string is supposed to represent a decimal number, and if it has 29 leading 0's, then those 0's will have to be removed before passing the string 30 to the `int` function, else that string will be tried to be parsed as an octal 31 number representation. 32 33 The [`strings.TrimLeft` function](/functions/strings.trimleft/) can be used for 34 this purpose. 35 36 ``` 37 {{ int ("0987" | strings.TrimLeft "0") }} 38 {{ int ("00987" | strings.TrimLeft "0") }} 39 ``` 40 41 **Explanation** 42 43 The `int` function eventually calls the `ParseInt` function from the Go library 44 `strconv`. 45 46 From its [documentation](https://golang.org/pkg/strconv/#ParseInt): 47 48 > the base is implied by the string's prefix: base 16 for "0x", base 8 for "0", 49 > and base 10 otherwise. 50 {{% /note %}}