如何在pinescript中将浮点数格式化为3位小数

ZKT*_*ON 3 pivot version fibonacci pine-script

我用它alertcondition来询问绘图中的值,但返回的值有超过 3 个小数位。

我可以格式化它以使输出只显示精度直到 3 位小数吗?

alertcondition(crossover(d2,d3), title="monitor", message='{{ticker}} Monitor If price above Current Price={{close}} PIVOT = {{plot("Pivot")}} SUPPORT = {{plot("S1")}} RESISTANCE = {{plot("R1")}}
Run Code Online (Sandbox Code Playgroud)

Pin*_*ucF 5

您需要对绘制的值进行四舍五入:

//@version=4
study("")
f_round( _val, _decimals) => 
    // Rounds _val to _decimals places.
    _p = pow(10,_decimals)
    round(abs(_val)*_p)/_p*sign(_val)
plot(close, "CloseP")
plot(f_round(close, 3), "CloseR")
alertcondition(true, "A", 'Unrounded: {{plot("CloseP")}}, Rounded:{{plot("CloseR")}}')
Run Code Online (Sandbox Code Playgroud)


Mic*_*_T. 5

如果您需要格式化字符串以仅显示三个十进制值,那么该tostring()函数的格式字符串参数将帮助您。

如果您需要以三个十进制值的精度绘制该值,那么您可能只需将研究的参数设置precision为 3。

使用这些参数的示例如下:

//@version=4
study("My Script", precision=3) // precision gives three decimal values in the plot-functions

// the format '#.###' gives three decimal values in a string
val = open / close
s = tostring(val, '#.###') 
label.new(bar_index, high, s)

// this plots only three decimal values, because of the study's param 'precision=3'
plot(val)
Run Code Online (Sandbox Code Playgroud)