Skip to main content
format(format, args...) produces a string by substituting values into a format template. It follows the same conventions as printf in most languages.

Signature

format(format string, args...) string

Format verbs

VerbDescription
%sString
%qJSON-quoted string
%dDecimal integer
%fFloating-point number
%gCompact floating-point (removes trailing zeros)
%GCompact floating-point (uppercase exponent)
%eScientific notation
%EScientific notation (uppercase)
%xHexadecimal (lowercase)
%XHexadecimal (uppercase)
%bBinary
%oOctal
%tBoolean (true or false)
%vAny value in a natural format
%%Literal %

Example

Build a Docker image tag with zero-padded build numbers:
variable "build_number" {
  description = "CI build number"
  type        = number
}

variable "app" {
  type    = string
  default = "api"
}

computed "image_tag" {
  description = "Padded build tag for Docker image"
  expression  = format("%s-build-%04d", var.app, var.build_number)
}

task "build" {
  description = "Build the Docker image with a build-stamped tag"
  commands    = ["docker build -t myapp:${computed.image_tag} ."]
}
With build_number = 7, computed.image_tag becomes api-build-0007.

Notes

  • format is most useful when you need padding, alignment, or number formatting. For simple concatenation, string interpolation (${}) is more readable.
  • An incorrect number of arguments or incompatible types produce an error at evaluation time.