Converts a string of digits to a float, giving na when the text is not a number.
str.tonumber reads an optional sign, digits and at most one decimal point. "12.5", "-3" and ".5" convert, while "1e5", "12abc" and an empty string give na.
The result is a float even for whole numbers. Wrap it in nz() when you want 0 instead of na on bad input.
Syntax
str.tonumber(string: series string) -> series float
str.tonumber(string: const string) -> const float
str.tonumber(string: input string) -> input float
str.tonumber(string: simple string) -> simple floatArguments
| Name | Type | Default | Description |
|---|---|---|---|
| string | const/ | required | Text holding a number, such as "12.5" or "-3". Anything else gives na. |
Returns
const/input/simple/series float: the number, or na if the text is not a plain number.
Remarks
- Thousands separators and currency signs make the text invalid. Remove them with
str.replace_allfirst.
Example
//@version=6
indicator("To number")
txt = str.tostring(close, "0.0")
plot(str.tonumber(txt), "Close, 1 decimal", color = color.green)
plot(nz(str.tonumber("abc"), -1), "Bad input", color = color.red)See also
Runs on Sidechair