如何在 Pine Script 中截断数字?

How can I truncate numbers in Pine Script?

我有这个 table,它显示了 RSI、ADX、DMI+ 和 DMI- 的值。但是,在逗号之后我有 10 个数字,但我只想要 2 个。 我怎样才能做到这一点?

//===== RSI =====//
var string GP2 = "RSI Settings"
lenRSI = input.int(14, inline = "11", minval=1, title="RSI Length", group = GP2)
src = input(close, "RSI Source", inline = "11", group = GP2)
upRSI = ta.rma(math.max(ta.change(src), 0), lenRSI)
downRSI = ta.rma(-math.min(ta.change(src), 0), lenRSI)
rsi = downRSI == 0 ? 100 : upRSI == 0 ? 0 : 100 - (100 / (1 + upRSI / downRSI))

//===== DMI+/- =====//
var string GP3 = "ADX and DMI +/- Settings"
lensig = input.int(14, inline = "11", title="ADX Smoothing", minval=1, maxval=50, group = GP3)
voltrig = input.int(20, inline = "11", minval=1, title="ADX Volatility Trigger", group = GP3)
lenDMI = input.int(14, inline = "11", minval=1, title="DMI +/- Length", group = GP3)
upDMI = ta.change(high)
downDMI = -ta.change(low)
plusDMI = na(upDMI) ? na : (upDMI > downDMI and upDMI > 0 ? upDMI : 0)
minusDMI = na(downDMI) ? na : (downDMI > upDMI and downDMI > 0 ? downDMI : 0)
trur = ta.rma(ta.tr, lenDMI)
plus = fixnan(100 * ta.rma(plusDMI, lenDMI) / trur)
minus = fixnan(100 * ta.rma(minusDMI, lenDMI) / trur)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)

//===== Table =====//
    var string GP4 = "Table Position"
string  tableYposInput = input.string("top", "Panel position", inline = "11", options = ["top", "middle", "bottom"], group = GP4)
string  tableXposInput = input.string("right", "", inline = "11", options = ["left", "center", "right"], group = GP4)

var testTable = table.new(position = position.top_right, columns = 4, rows = 3, bgcolor = color.rgb(250, 250, 250, 100), frame_color = color.rgb(250, 250, 250, 90), frame_width = 2, border_color = color.rgb(250, 250, 250, 90), border_width = 2)
if barstate.islast
    table.cell(table_id = testTable, column = 0, row = 0, text = "RSI", text_color = color.rgb(255, 186, 8))
    table.cell(table_id = testTable, column = 1, row = 0, text = "ADX", text_color = color.rgb(255, 186, 8))
    table.cell(table_id = testTable, column = 2, row = 0, text = "DMI+", text_color = color.rgb(255, 186, 8))
    table.cell(table_id = testTable, column = 3, row = 0, text = "DMI-", text_color = color.rgb(255, 186, 8))
    table.cell(table_id = testTable, column = 0, row = 1, text = "" + str.tostring(rsi), text_color = color.rgb(255, 186, 8))
    table.cell(table_id = testTable, column = 1, row = 1, text = "" + str.tostring(adx), text_color = color.rgb(255, 186, 8))
    table.cell(table_id = testTable, column = 2, row = 1, text = "" + str.tostring(plus), text_color = color.rgb(255, 186, 8))
    table.cell(table_id = testTable, column = 3, row = 1, text = "" + str.tostring(minus), text_color = color.rgb(255, 186, 8))

您可以使用 str.tostring(value, format) 函数的格式参数。

str.tostring(rsi, ".##")
table.cell(table_id = testTable, column = 0, row = 1, text = "" + str.tostring(int(rsi)), text_color = color.rgb(255, 186, 8))
table.cell(table_id = testTable, column = 1, row = 1, text = "" + str.tostring(int(adx)), text_color = color.rgb(255, 186, 8))
table.cell(table_id = testTable, column = 2, row = 1, text = "" + str.tostring(int(plus)), text_color = color.rgb(255, 186, 8))
table.cell(table_id = testTable, column = 3, row = 1, text = "" + str.tostring(int(minus)), text_color = color.rgb(255, 186, 8))