float() - Pine Script Function
float()
Section titled “float()”Overview
Section titled “Overview”float() casts a value to the floating-point type. It is useful when you receive generic inputs (strings, ints, na) and need a numeric result for calculations.
Syntax
Section titled “Syntax”float(x) → const floatParameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
x | const int/float/bool/string | Value to convert to float. |
Returns
Section titled “Returns”const float — result of the cast (na if conversion is not possible).
Remarks
Section titled “Remarks”naconverts tona.true→1.0,false→0.0.- Strings must be numeric (e.g.,
"12.5"); otherwise, the result isna.
Example
Section titled “Example”//@version=5indicator("float() example", overlay=false)
strInput = input.string("12.5", "String input")boolInput = input.bool(true, "Bool input")
floatFromString = float(strInput)floatFromBool = float(boolInput)
plot(floatFromString, "Float from string", color=color.blue)plot(floatFromBool, "Float from bool", color=color.orange)