mirror of
https://github.com/gohugoio/hugoDocs.git
synced 2025-09-11 08:54:41 -04:00
Clarify cast functions (#2041)
This commit is contained in:
parent
03fd1d4043
commit
2a37a1d218
@ -1,17 +1,45 @@
|
||||
---
|
||||
title: float
|
||||
description: Creates a `float` from the argument passed into the function.
|
||||
description: Casts a value to a decimal (base 10) floating point value.
|
||||
categories: [functions]
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
keywords: [strings,floats]
|
||||
keywords: [cast,strings,floats]
|
||||
signature: ["float INPUT"]
|
||||
relatedfuncs: []
|
||||
---
|
||||
|
||||
Useful for turning strings into floating point numbers.
|
||||
With a decimal (base 10) input:
|
||||
|
||||
```go-html-template
|
||||
{{ float "1.23" }} → 1.23
|
||||
{{ float 11 }} → 11 (float64)
|
||||
{{ float "11" }} → 11 (float64)
|
||||
|
||||
{{ float 11.1 }} → 11.1 (float64)
|
||||
{{ float "11.1" }} → 11.1 (float64)
|
||||
|
||||
{{ float 11.9 }} → 11.9 (float64)
|
||||
{{ float "11.9" }} → 11.9 (float64)
|
||||
```
|
||||
|
||||
With a binary (base 2) input:
|
||||
|
||||
```go-html-template
|
||||
{{ float 0b11 }} → 3 (float64)
|
||||
```
|
||||
|
||||
With an octal (base 8) input (use either notation):
|
||||
|
||||
```go-html-template
|
||||
{{ float 011 }} → 9 (float64)
|
||||
{{ float "011" }} → 11 (float64)
|
||||
|
||||
{{ float 0o11 }} → 9 (float64)
|
||||
```
|
||||
|
||||
With a hexadecimal (base 16) input:
|
||||
|
||||
```go-html-template
|
||||
{{ float 0x11 }} → 17 (float64)
|
||||
```
|
||||
|
@ -1,41 +1,51 @@
|
||||
---
|
||||
title: int
|
||||
description: Creates an `int` from the argument passed into the function.
|
||||
description: Casts a value to a decimal (base 10) integer.
|
||||
categories: [functions]
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
keywords: [strings,integers]
|
||||
keywords: [cast,strings,integers]
|
||||
signature: ["int INPUT"]
|
||||
relatedfuncs: []
|
||||
---
|
||||
|
||||
Useful for turning strings into numbers.
|
||||
With a decimal (base 10) input:
|
||||
|
||||
```go-html-template
|
||||
{{ int "123" }} → 123
|
||||
{{ int 11 }} → 11 (int)
|
||||
{{ int "11" }} → 11 (int)
|
||||
|
||||
{{ int 11.1 }} → 11 (int)
|
||||
{{ int 11.9 }} → 11 (int)
|
||||
```
|
||||
|
||||
{{% note "Usage Note" %}}
|
||||
If the input string is supposed to represent a decimal number, and if it has
|
||||
leading 0's, then those 0's will have to be removed before passing the string
|
||||
to the `int` function, else that string will be tried to be parsed as an octal
|
||||
number representation.
|
||||
|
||||
The `strings.TrimLeft` can be used for this purpose.
|
||||
With a binary (base 2) input:
|
||||
|
||||
```go-html-template
|
||||
{{ int ("0987" | strings.TrimLeft "0") }}
|
||||
{{ int ("00987" | strings.TrimLeft "0") }}
|
||||
{{ int 0b11 }} → 3 (int)
|
||||
{{ int "0b11" }} → 3 (int)
|
||||
```
|
||||
|
||||
### Explanation
|
||||
With an octal (base 8) input (use either notation):
|
||||
|
||||
The `int` function eventually calls the `ParseInt` function from the Go library
|
||||
`strconv`.
|
||||
```go-html-template
|
||||
{{ int 011 }} → 9 (int)
|
||||
{{ int "011" }} → 9 (int)
|
||||
|
||||
From its [documentation](https://golang.org/pkg/strconv/#ParseInt):
|
||||
{{ int 0o11 }} → 9 (int)
|
||||
{{ int "0o11" }} → 9 (int)
|
||||
```
|
||||
|
||||
> the base is implied by the string's prefix: base 16 for "0x", base 8 for "0",
|
||||
> and base 10 otherwise.
|
||||
With a hexadecimal (base 16) input:
|
||||
|
||||
```go-html-template
|
||||
{{ int 0x11 }} → 17 (int)
|
||||
{{ int "0x11" }} → 17 (int)
|
||||
```
|
||||
|
||||
{{% note %}}
|
||||
Values with a leading zero are octal (base 8). When casting a string representation of a decimal (base 10) number, remove leading zeros:
|
||||
|
||||
`{{ strings.TrimLeft "0" "0011" | int }} → 11`
|
||||
{{% /note %}}
|
||||
|
@ -1,13 +1,48 @@
|
||||
---
|
||||
title: string
|
||||
description: Creates a string from the argument passed to the function
|
||||
description: Cast a value to a string.
|
||||
categories: [functions]
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
keywords: [strings]
|
||||
keywords: [cast,strings]
|
||||
signature: ["string INPUT"]
|
||||
relatedfuncs: []
|
||||
---
|
||||
|
||||
* `{{ string "BatMan" }}` → "BatMan"
|
||||
With a decimal (base 10) input:
|
||||
|
||||
```go-html-template
|
||||
{{ string 11 }} → 11 (string)
|
||||
{{ string "11" }} → 11 (string)
|
||||
|
||||
{{ string 11.1 }} → 11.1 (string)
|
||||
{{ string "11.1" }} → 11.1 (string)
|
||||
|
||||
{{ string 11.9 }} → 11.9 (string)
|
||||
{{ string "11.9" }} → 11.9 (string)
|
||||
```
|
||||
|
||||
With a binary (base 2) input:
|
||||
|
||||
```go-html-template
|
||||
{{ string 0b11 }} → 3 (string)
|
||||
{{ string "0b11" }} → 0b11 (string)
|
||||
```
|
||||
|
||||
With an octal (base 8) input (use either notation):
|
||||
|
||||
```go-html-template
|
||||
{{ string 011 }} → 9 (string)
|
||||
{{ string "011" }} → 011 (string)
|
||||
|
||||
{{ string 0o11 }} → 9 (string)
|
||||
{{ string "0o11" }} → 0o11 (string)
|
||||
```
|
||||
|
||||
With a hexadecimal (base 16) input:
|
||||
|
||||
```go-html-template
|
||||
{{ string 0x11 }} → 17 (string)
|
||||
{{ string "0x11" }} → 0x11 (string)
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user