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
2and62. Most of the time, you will use base10(decimal).
💻 Syntax
💡 Examples
1. Basic Decimal Conversion:
2. Hexadecimal to Decimal:
Useful when dealing with hardware IDs or color codes.
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. |
🔗 Related Functions
-
[[abs]] — Get the absolute value of the result.
-
[[floor]] — If you have a float string like "12.5", you can't use
parseintdirectly.