Home » String Functions » Introduction to str.tonumber() Function in Pine Script

Introduction to str.tonumber() Function in Pine Script

Photo of author
Published on

In this article, we will delve into how to use the str.tonumber() function in Pine Script version 5, providing comprehensive examples and explanations.

Using str.tonumber() Function

The str.tonumber() function in Pine Script version 5 is designed to convert a string that represents a number into a numeric value. If the string cannot be converted to a number, the function returns na (not a number). This is particularly useful when dealing with data that comes in text format but needs to be used in numerical calculations.

Syntax

numericValue = str.tonumber(string)
  • string: The string input that you want to convert to a number.

Example

Let’s consider an example where we have a string variable that contains a number, and we want to convert this string to a numeric value to perform mathematical operations.

//@version=5
indicator("String to Number Conversion", overlay=true)

// Define a string variable that represents a number
stringNumber = "123.45"

// Convert the string to a numeric value
numericValue = str.tonumber(stringNumber)

// Plot the numeric value on the chart
plot(numericValue, title="Numeric Value", color=color.blue)
Example

In this example, stringNumber is a string variable that holds the value "123.45". We use the str.tonumber(stringNumber) function to convert it into a numeric value, which is then stored in the variable numericValue. This numeric value is plotted on the chart, demonstrating the conversion from string to number.

Detailed Walkthrough

  1. Script Version Declaration:
    • //@version=5 indicates that this script is written for Pine Script version 5, ensuring compatibility with the latest features and functionalities offered by TradingView’s Pine Script language.
  2. Indicator Declaration:
    • indicator("String to Number Conversion", overlay=true) defines a new indicator titled “String to Number Conversion”. The overlay=true parameter specifies that this indicator will be plotted directly on the trading chart, overlaying the price data.
  3. String Variable Definition:
    • stringNumber = "123.45" declares a variable named stringNumber and assigns it a string value of "123.45". This string represents a numeric value (in this case, a floating-point number) as text.
  4. String to Numeric Conversion:
    • numericValue = str.tonumber(stringNumber) uses the str.tonumber() function to convert the string stored in stringNumber to a numeric value. The result of this conversion is stored in a new variable named numericValue.
    • This conversion is crucial for further mathematical or analytical operations, as it transforms the textual representation of a number into a format that can be numerically manipulated.
  5. Plotting the Numeric Value:
    • plot(numericValue, title="Numeric Value", color=color.blue) plots the numeric value obtained from the conversion on the chart. The plot function is used for this purpose, with numericValue as the data to be plotted.
    • The title="Numeric Value" parameter specifies the title of the plot, which will be visible on the chart’s legend.
    • The color=color.blue parameter sets the color of the plot to blue, enhancing the visual distinction of this plotted data on the chart.

Key Features and Takeaways

  • Function Useability: str.tonumber() is a versatile function in Pine Script version 5 for converting strings that represent numbers into numeric values, enhancing the script’s ability to handle dynamic data types.
  • Syntax and Application: The function is straightforward to use, with a single argument that is the string to be converted. It returns a numeric value if successful or na if the conversion fails.
  • Error Handling: Implementing error checking when using str.tonumber() is crucial for robust script development, allowing for graceful handling of non-numeric strings or malformed inputs.

Leave a Comment