Skip to main content
tonumber(v) converts v to a number. Numeric strings like "42" or "3.14" are parsed. Numbers pass through unchanged.

Signature

tonumber(v any) number

Example

Read a timeout value from an environment variable and use it as a number in a command:
computed "timeout" {
  description = "Connection timeout in seconds"
  expression  = tonumber(env("CONNECT_TIMEOUT", "30"))
}

task "health-check" {
  description = "Wait for the service to be ready"
  commands    = ["./bin/wait-for-ready --timeout ${computed.timeout}"]
}

Notes

  • The string must be a valid decimal number. Non-numeric strings produce an error.
  • For integers in non-decimal bases (hex, binary), use parseint instead.
  • tonumber is most useful when a numeric value comes from an environment variable or a string expression.