2019-01-17 18:11:48 +01:00
|
|
|
---
|
2021-11-23 00:57:25 +01:00
|
|
|
layout: "language"
|
|
|
|
page_title: "tonumber - Functions - Configuration Language"
|
|
|
|
sidebar_current: "docs-funcs-conversion-tonumber"
|
|
|
|
description: |-
|
|
|
|
The tonumber function converts a value to a number.
|
2019-01-17 18:11:48 +01:00
|
|
|
---
|
|
|
|
|
|
|
|
# `tonumber` Function
|
|
|
|
|
|
|
|
`tonumber` converts its argument to a number value.
|
|
|
|
|
|
|
|
Explicit type conversions are rarely necessary in Terraform because it will
|
|
|
|
convert types automatically where required. Use the explicit type conversion
|
|
|
|
functions only to normalize types returned in module outputs.
|
|
|
|
|
2021-04-10 21:54:51 +02:00
|
|
|
Only numbers, `null`, and strings containing decimal representations of numbers can be
|
2019-01-17 18:11:48 +01:00
|
|
|
converted to number. All other values will produce an error.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
> tonumber(1)
|
|
|
|
1
|
|
|
|
> tonumber("1")
|
|
|
|
1
|
2021-04-10 21:54:51 +02:00
|
|
|
> tonumber(null)
|
|
|
|
null
|
2019-01-17 18:11:48 +01:00
|
|
|
> tonumber("no")
|
|
|
|
Error: Invalid function argument
|
|
|
|
|
|
|
|
Invalid value for "v" parameter: cannot convert "no" to number: string must be
|
|
|
|
a decimal representation of a number.
|
|
|
|
```
|