Skip to content
Algo Trade Analytics Docs

str.format() - Pine Script Function

Creates a formatted string using a specified formatting string (formatString) and one or more additional arguments (arg0, arg1, etc.). The formatting string defines the structure of the returned string, where all placeholders in curly brackets ({}) refer to the additional arguments. Each placeholder requires a number representing an argument’s position, starting from 0. For instance, the placeholder {0} refers to the first argument after formatString (arg0), {1} refers to the second (arg1), and so on. The function replaces each placeholder with a string representation of the corresponding argument.

str.format(formatString, arg0, arg1, ...) → simple string
NameTypeDescription
formatStringsimple stringFormat string.

The formatted string.

  • The string used as the formatString argument can contain single quote characters (’). However, programmers must pair all single quotes in that string to avoid unexpected formatting results.
//@version=6
indicator("Extensive `str.format()` demo", overlay=true)// The format specifier inside the curly braces accepts certain modifiers:// - Specify the number of decimals to display:s1 = str.format("{0,number,#.#}", 1.34) // returns: 1.3 label.new(bar_index, close, text=s1)// - Round a float value to an integer:s2 = str.format("{0,number,integer}", 1.34) // returns: 1 label.new(bar_index - 1, close, text=s2)// - Display a number in currency:s3 = str.format("{0,number,currency}", 1.34) // returns: $1.34 label.new(bar_index - 2, close, text=s3)// - Display a number as a percentage:s4 = str.format("{0,number,percent}", 0.5) // returns: 50% label.new(bar_index - 3, close, text=s4)// EXAMPLES WITH SEVERAL ARGUMENTS// returns: Number 1 is not equal to 4s5 = str.format("Number {0}
is not {1}
to {2}", 1, "equal", 4)
label.new(bar_index - 4, close, text=s5)// returns: 1.34 != 1.3s6 = str.format("{0} != {0, number, #.#}", 1.34)
label.new(bar_index - 5, close, text=s6)// returns: 1 is equal to 1, but 2 is equal to 2s7 = str.format("{0, number, integer}
is equal to 1, but {1, number, integer}
is equal to 2", 1.34, 1.52)
label.new(bar_index - 6, close, text=s7)// returns: The cash turnover amounted to $1,340,000.00s8 = str.format("The cash turnover amounted to {0, number, currency}", 1340000)
label.new(bar_index - 7, close, text=s8)// returns: Expected
return is 10% - 20%s9 = str.format("Expected
return is {0, number, percent} - {1, number, percent}", 0.1, 0.2)
label.new(bar_index - 8, close, text=s9)