github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/website/content/docs/job-specification/hcl2/functions/string/format.mdx (about) 1 --- 2 layout: docs 3 page_title: format - Functions - Configuration Language 4 description: |- 5 The format function produces a string by formatting a number of other values 6 according to a specification string. 7 --- 8 9 # `format` Function 10 11 `format` produces a string by formatting a number of other values according 12 to a specification string. It is similar to the `printf` function in C, and 13 other similar functions in other programming languages. 14 15 ```hcl 16 format(spec, values...) 17 ``` 18 19 ## Examples 20 21 ```shell-session 22 > format("Hello, %s!", "Ander") 23 Hello, Ander! 24 > format("There are %d lights", 4) 25 There are 4 lights 26 ``` 27 28 Simple format verbs like `%s` and `%d` behave similarly to template 29 interpolation syntax, which is often more readable: 30 31 ```shell-session 32 > format("Hello, %s!", var.name) 33 Hello, Valentina! 34 > "Hello, ${var.name}!" 35 Hello, Valentina! 36 ``` 37 38 The `format` function is therefore more useful when you use more complex format 39 specifications, as described in the following section. 40 41 ## Specification Syntax 42 43 The specification is a string that includes formatting verbs that are introduced 44 with the `%` character. The function call must then have one additional argument 45 for each verb sequence in the specification. The verbs are matched with 46 consecutive arguments and formatted as directed, as long as each given argument 47 is convertible to the type required by the format verb. 48 49 The specification may contain the following verbs: 50 51 | Verb | Result | 52 | ----- | ----------------------------------------------------------------------------------------- | 53 | `%%` | Literal percent sign, consuming no value. | 54 | `%v` | Default formatting based on the value type, as described below. | 55 | `%#v` | JSON serialization of the value, as with `jsonencode`. | 56 | `%t` | Convert to boolean and produce `true` or `false`. | 57 | `%b` | Convert to integer number and produce binary representation. | 58 | `%d` | Convert to integer number and produce decimal representation. | 59 | `%o` | Convert to integer number and produce octal representation. | 60 | `%x` | Convert to integer number and produce hexadecimal representation with lowercase letters. | 61 | `%X` | Like `%x`, but use uppercase letters. | 62 | `%e` | Convert to number and produce scientific notation, like `-1.234456e+78`. | 63 | `%E` | Like `%e`, but use an uppercase `E` to introduce the exponent. | 64 | `%f` | Convert to number and produce decimal fraction notation with no exponent, like `123.456`. | 65 | `%g` | Like `%e` for large exponents or like `%f` otherwise. | 66 | `%G` | Like `%E` for large exponents or like `%f` otherwise. | 67 | `%s` | Convert to string and insert the string's characters. | 68 | `%q` | Convert to string and produce a JSON quoted string representation. | 69 70 When `%v` is used, one of the following format verbs is chosen based on the value type: 71 72 | Type | Verb | 73 | --------- | ----- | 74 | `string` | `%s` | 75 | `number` | `%g` | 76 | `bool` | `%t` | 77 | any other | `%#v` | 78 79 Null values produce the string `null` if formatted with `%v` or `%#v`, and 80 cause an error for other verbs. 81 82 A width modifier can be included with an optional decimal number immediately 83 preceding the verb letter, to specify how many characters will be used to 84 represent the value. Precision can be specified after the (optional) width 85 with a period (`.`) followed by a decimal number. If width or precision are 86 omitted then default values are selected based on the given value. For example: 87 88 | Sequence | Result | 89 | -------- | ---------------------------- | 90 | `%f` | Default width and precision. | 91 | `%9f` | Width 9, default precision. | 92 | `%.2f` | Default width, precision 2. | 93 | `%9.2f` | Width 9, precision 2. | 94 95 The following additional symbols can be used immediately after the `%` symbol 96 to set additoinal flags: 97 98 | Symbol | Result | 99 | ------ | -------------------------------------------------------------- | 100 | space | Leave a space where the sign would be if a number is positive. | 101 | `+` | Show the sign of a number even if it is positive. | 102 | `-` | Pad the width with spaces on the left rather than the right. | 103 | `0` | Pad the width with leading zeros rather than spaces. | 104 105 By default, `%` sequences consume successive arguments starting with the first. 106 Introducing a `[n]` sequence immediately before the verb letter, where `n` is a 107 decimal integer, explicitly chooses a particular value argument by its 108 one-based index. Subsequent calls without an explicit index will then proceed 109 with `n`+1, `n`+2, etc. 110 111 The function produces an error if the format string requests an impossible 112 conversion or access more arguments than are given. An error is produced also 113 for an unsupported format verb. 114 115 ## Related Functions 116 117 - [`formatdate`](/docs/job-specification/hcl2/functions/datetime/formatdate) is a specialized formatting function for 118 human-readable timestamps. 119 - [`formatlist`](/docs/job-specification/hcl2/functions/string/formatlist) uses the same specification syntax to 120 produce a list of strings.