[Stock][TradingView][教學][Pine Script] 更方便的字串格式化函數str.format()
目的
Pine Script終於提供更方便的字串格式化的函數str.format()這個函數會讓你更方便省時來顯
示你要呈現的文字 。
如果文章對你有幫助在幫我按一下廣告來讓我有額外收入這也是對我來說是一種鼓勵。
說明
以下是官方提供的函數第一個參數formatString就是要格式化的內容,然後參數可以有多個
arg0, arg1.....
str.format(formatString, arg0, arg1, ...) -> stringcomment = str.format("今日收盤價{0},今日最高價{1}", close, high)接著str.format()也提供了數值跟日期/時間和時區的格式化(以下內容為官方提供)
1. 數值
* 限制小數點的顯示
str.format("{0,number,#.#}", 1.34) // returns: 1.3 * 移除小數點
str.format("{0,number,integer}", 1.34) // returns: 1* 顯示貨幣格式
str.format("The cash turnover amounted to {0,number,currency}", 1340000)
// “The cash turnover amounted to $1,340,000.00”.* 顯示百分比
str.format("Expected return is {0,number,percent} - {1,number,percent}", 0.1, 0.2)
// returns: Expected return is 10% - 20%.2. 日期/時間和時區
* 顯示
str.format("Current bar date: {0,date, Y-MM-d}", timenow)str.format("Current bar date: {0,date, '今天是'M'月的第' F '週,今年的第'D'天'}", timenow)
// 今天是6月的第4週,今年的第179以下是支援的格式
{0,date,y.MM.dd hh:mm:ss}
{1,date,short}
{2,date,medium}
{3,date,long}
{4,date,full}
{5,date,h a z (zzzz)}
{6,time,short}
{7,time,medium}
{8,time,long}
{9,time,full}
{10,time,hh:mm:ss}
{11,time,HH:mm:ss}參考:https://www.tradingview.com/blog/tw/new-pine-funtions-for-strings-and-arrays-24554/

留言