str.tostring Pine Script: A Concise Guide for Traders and Developers
In Pine Script, the scripting language behind TradingView charts, the function str.tostring() plays a crucial role in converting various data types into string format. This conversion is essential for displaying data, creating labels, or logging information on charts, making str.tostring() a versatile tool for traders and developers alike.
What is str.tostring() in Pine Script?
The str.tostring() function converts inputs such as integers, floats, booleans, strings, arrays, or matrices into their string representations. This allows you to manipulate and present data in a readable text format on TradingView charts.

Syntax
str.tostring(value)
Converts the given value to a string.str.tostring(value, format)
Converts the value to a string with specific formatting options.
Formatting Options
The format
parameter accepts constants like:
format.mintick
- rounds to the nearest minimum tick size of the symbol.format.percent
- formats the number as a percentage.format.volume
- formats numbers as volume.- Custom patterns like
"#.000"
to control decimal places and trailing zeros.
For example, str.tostring(3.99, "#.000")
returns "4.000"
.
Return Values
- If the input is already a string, it is returned unchanged.
- Boolean values return
"true"
or"false"
. - If the input is
na
(not available), it returns"NaN"
.
Why Use str.tostring()?
- Display numeric data as text: Useful for labeling prices, indicators, or other values on charts.
- Format data precisely: Control decimal places or rounding to match the symbol's tick size.
- Convert booleans and arrays: For debugging or displaying complex data structures.
Example Usage
//@version=5
indicator("Example str.tostring", overlay=true)
value = close
strValue = str.tostring(value, format.mintick)
plot(value)
label.new(bar_index, value, text=strValue, size=size.large)
This script plots the closing price and creates a label showing the price as a formatted string.
Common Issues and Tips
- "Could Not Find The Function Tostring" Error:
This error often occurs if you try to use
tostring()
instead ofstr.tostring()
, or if your Pine Script version is outdated (pre-version 4). Always usestr.tostring()
and ensure your script starts with//@version=4
or higher. - Resource Usage:
The
str.tostring()
function can be CPU-intensive if overused, especially in scripts with many labels or logs. Optimize by minimizing calls tostr.tostring()
and avoid unnecessary string conversions. - Debugging:
Use
label.new()
orplot()
to display intermediate string conversions and verify correctness.