Skip to content

Function parseint()

📝 Description

The parseint function parses a string as an integer of a specified base. It converts text-based numbers into actual numeric values that Terraform can use for calculations.

Note: The base must be between 2 and 62. Most of the time, you will use base 10 (decimal).


💻 Syntax

parseint(string, base)

💡 Examples

1. Basic Decimal Conversion:

# Result: 100 (number)
output "decimal" {
  value = parseint("100", 10)
}

2. Hexadecimal to Decimal:

Useful when dealing with hardware IDs or color codes.

# Result: 255
output "hex" {
  value = parseint("FF", 16)
}

3. Practical Scenario: API Data Conversion

Imagine you get a string value from an external data source or a tag, and you need to use it in a math operation:

variable "string_count" { default = "05" }

locals {
  # Terraform would fail if you try to add 1 to a string "05"
  # parseint converts it to a clean number 5
  actual_count = parseint(var.string_count, 10) + 1 # Result: 6
}

⚠️ Common Errors

Input Result Why?
parseint("12.5", 10) Error It only parses whole integers, not floats.
parseint("ABC", 10) Error "ABC" is not a valid decimal number.
parseint("100", 1) Error Base must be at least 2.

  • [[abs]] — Get the absolute value of the result.

  • [[floor]] — If you have a float string like "12.5", you can't use parseint directly.

  • Official HashiCorp Docs