Sidechair

str.tostring()

Browse the reference

Converts a number, boolean, string or array to text, optionally through a format pattern or a format.* constant.

Try it in the editor

str.tostring turns a value into a string. Booleans become true or false, strings pass through, and an array becomes its items in square brackets, separated by commas.

A number prints with up to eight decimals and no trailing zeros unless you pass a format. The format is either a pattern such as "#.##" or "0.000", or one of format.price, format.mintick, format.percent and format.volume.

Syntax

Pine Script
str.tostring(value: series int/float/bool/string/enum/array<int/float/bool/string>/matrix<int/float/bool/string>) -> series string
str.tostring(value: simple int/float/bool/string/enum) -> simple string
str.tostring(value: const enum) -> const string
str.tostring(value: series int/float/array<int/float>/matrix<int/float>, format: series string) -> series string
str.tostring(value: simple int/float, format: simple string) -> simple string

Arguments

NameTypeDefaultDescription
valueseries int/float/bool/string/enum/array<int/float/bool/string>/matrix<int/float/bool/string> or simple int/float/bool/string/enum or const enum or series int/float/array<int/float>/matrix<int/float> or simple int/floatrequiredThe value to convert: a number, bool, string, enum or an array of them.
formatsimple/series stringrequiredA number pattern, or a format.* constant. Digits in the pattern set the decimals shown, 0 forces a digit and # shows one only when needed.

Returns

const/simple/series string: the value as text.

Remarks

  • format.price rounds to the symbol's decimals, format.mintick rounds to the tick size, format.percent shows two decimals with a percent sign, and format.volume abbreviates with K, M and B.
  • An na number becomes the text NaN.
  • On Sidechair: a pattern follows Java DecimalFormat rules, so "#.##" prints 3 for 3.001 and a result that rounds to zero carries no minus sign.

Example

Pine ScriptDigits in the closeOpen in editor
//@version=6
indicator("Close as text")
txt = str.tostring(close, "#.00")
plot(str.length(txt), "Characters", color = color.blue, style = plot.style_stepline)
plot(str.tonumber(str.tostring(close, format.mintick)), "Rounded close", color = color.orange)

See also

Runs on Sidechair