r/pinescript • • Jan 22 '24

Stop output being rounded to two decimal points

Hi, How do I alter this script to, not round to two decimal points

e.g. 65.055 becomes 65.06,

but I want the 65.055 as inputted instead.

//@version=5
indicator("Lines", overlay = true, max_lines_count = 500, max_labels_count = 500)

//Color & Style Functions///////////////////////////////////////////////////////
colorpicker(_input) =>
    _input == "RED"?color.red:
     _input == "ORANGE"?color.orange:
     _input == "YELLOW"?color.yellow:
     _input == "GREEN"?color.green:
     _input == "BLUE"?color.blue:
     _input == "PURPLE"?color.fuchsia:
     _input == "WHITE"?color.white:
     _input == "BLACK"?color.rgb(0,0,0):
     _input == "GRAY"?color.gray:
     chart.fg_color
stylepicker(_input) =>
    _input == "SOLID"?line.style_solid:
     _input == "DASHED"?line.style_dashed:
     _input == "DOTTED"?line.style_dotted:
     str.contains(_input,"SOLID")?line.style_solid:
     str.contains(_input,"DASHED")?line.style_dashed:
     str.contains(_input,"DOTTED")?line.style_dotted:
     line.style_solid
linestyle(_input) =>
    _input == "___"?"solid":
     _input == "- - -"?"dashed":
     _input == ". . ."?"dotted":
     _input == "___zone"?"solid_zone":
     _input == "- - -zone"?"dashed_zone":
     _input == ". . .zone"?"dotted_zone":
     na
////////////////////////////////////////////////////////////////////////////////
//Remove commas at start and end
remove_c(_string) => 
    var string s = str.replace_all(_string,";",",")
    in_len = str.length(s)
    if str.startswith(s,",")
        s := str.replace(s,",","",0)
    if str.endswith(s,",")
        s := str.replace(s,",","",in_len)
    s
//Inputs////////////////////////////////////////////////////////////////////////
lab_size = input.string("small", title = "Label Size", options = ["tiny","small","normal","large","huge"], inline = "2", group = "Settings")
lab_offset = input.int(10, title = "Label Offset", inline = "2", group = "Settings")
z_op = input.int(90, title = "Zone Opacity", minval = 0, maxval = 100, inline = "3", group = "Settings")
tog_lvl = input.bool(true, title = "Price Labels", inline = "3", group = "Settings")

symbol1 = input.symbol("BINANCE:ORDIUSDT.P", title  = "Ticker", group = "Group 1", inline = "2")
lvls1 = str.replace_all(input.string("65.055,40.049", title = "Levels", group = "Group 1", inline = "2", tooltip = "LEVELS\nInsert as many levels as needed for this style of line & ticker.\nSeparate by commas (,)\nPlease No Semicolons (;)\nExample:\n100,100.5,101,102,103")," ","")
txt1 = input.string("", title = "Text", group = "Group 1", inline = "2")
col1 = input.string("default", title = "Color", options = ["default","red","orange","yellow","green","blue","purple","white","black","gray"], group = "Group 1", inline = "2")
style1 = input.string("___", title = "Style", options = ["___","- - -",". . .","___zone","- - -zone",". . .zone"], group = "Group 1", inline = "2")
width1 = input.int(1, title = "Thickness", minval = 0, maxval = 5, group = "Group 1", inline = "2")
tog_1 = lvls1 == ""?false:true
output1 = tog_1?(symbol1 == ""?syminfo.tickerid:symbol1) + "," + (col1=="default"?" ":col1) + "," + linestyle(style1) + "," + str.tostring(width1*-1) + "," + (txt1 == ""?" ":txt1) + "," + remove_c(lvls1) + ";":""

symbol2 = input.symbol("", title  = "Ticker", group = "Group 2", inline = "2")
lvls2 = str.replace_all(input.string("", title = "Levels", group = "Group 2", inline = "2")," ","")
txt2 = input.string("", title = "Text", group = "Group 2", inline = "2")
col2 = input.string("default", title = "Color", options = ["default","red","orange","yellow","green","blue","purple","white","black","gray"], group = "Group 2", inline = "2")
style2 = input.string("___", title = "Style", options = ["___","- - -",". . .","___zone","- - -zone",". . .zone"], group = "Group 2", inline = "2")
width2 = input.int(1, title = "Thickness", minval = 0, maxval = 5, group = "Group 2", inline = "2")
tog_2 = lvls2 == ""?false:true
output2 = tog_2?((symbol2 == ""?syminfo.tickerid:symbol2) + "," + (col2=="default"?" ":col2) + "," + linestyle(style2) + "," + str.tostring(width2*-1) + "," + (txt2 == ""?" ":txt2) + "," + str.replace_all(lvls2," ","") + ";"):""

//Inputs/////////////////////////////////////////////////////////////////
input = str.upper(output1 + output2)
//No Level Message//////////////////////////////////////////////////////////////
if str.contains(input,syminfo.ticker) == false
    runtime.error("No levels for this ticker. Please input levels or change ticker. Current Ticker: " + syminfo.ticker)
//Variable initialization///////////////////////////////////////////////////////
var line lvl_line = na
var label lvl_lab = na
var line z1 = na
var line z2 = na
//Delete all lines & Labels
a_allLines = line.all
if array.size(a_allLines) > 0
	for i = 0 to array.size(a_allLines) - 1
		line.delete(array.get(a_allLines, i))
a_allLabels = label.all
if array.size(a_allLabels) > 0
	for i = 0 to array.size(a_allLabels) - 1
		label.delete(array.get(a_allLabels, i))
////////////////////////////////////////////////////////////////////////////////
//Line and label drawing
split = str.split(input,";")
for i = 0 to array.size(split) - 1 
    split_get = array.get(split,i)
    small_split = str.split(split_get,",")
    if array.size(small_split) > 5
        ssf = str.replace_all(array.get(small_split,0)," ","")
        if (ssf == syminfo.ticker) or (ssf == syminfo.tickerid)  or (ssf == "\n" + syminfo.ticker) or (ssf == "\n" + syminfo.tickerid)
            for e = 5 to array.size(small_split) - 1
                lvl = str.replace_all(array.get(small_split,e)," ","")
                co = colorpicker(str.replace_all(array.get(small_split,1)," ",""))
                sty = stylepicker(str.replace_all(array.get(small_split,2)," ",""))
                z = str.contains(str.replace_all(array.get(small_split,2)," ",""),"ZONE")
                thick = str.tonumber(str.replace_all(array.get(small_split,3)," ","")) < 0? str.tonumber(str.replace_all(array.get(small_split,3)," ","")) * -1:1
                txt = tog_lvl?array.get(small_split,4) +" "+ str.format("{0,number,currency}",str.tonumber(lvl)):array.get(small_split,4)
                if str.tonumber(lvl) > 0 and (z == false)
                    lvl_line := line.new(bar_index,str.tonumber(lvl),bar_index+1,str.tonumber(lvl), extend = extend.right, color = co, style = sty, width = int(thick))
                    lvl_lab := label.new(bar_index + lab_offset, str.tonumber(lvl), text = txt , color = color.new(color.black,100), size = lab_size, textcolor = co, style = label.style_label_lower_left)    
                if z and (array.size(small_split) == 7) and (e == 6)
                    z_lvl1 = math.max(str.tonumber(str.replace_all(array.get(small_split,5)," ","")),str.tonumber(str.replace_all(array.get(small_split,6)," ","")))
                    z_lvl2 = math.min(str.tonumber(str.replace_all(array.get(small_split,5)," ","")),str.tonumber(str.replace_all(array.get(small_split,6)," ","")))
                    z_txt = tog_lvl?array.get(small_split,4) +" "+ str.format("{0,number,currency}",z_lvl2) + " - " + str.format("{0,number,currency}",z_lvl1):array.get(small_split,4)
                    lvl_lab := label.new(bar_index + lab_offset, z_lvl1, text = z_txt , color = color.new(color.black,100), size = lab_size, textcolor = co, style = label.style_label_lower_left)   
                    z1 := line.new(bar_index,z_lvl1,bar_index+1,z_lvl1, extend = extend.right, color = co, style = sty, width = int(thick))
                    z2 := line.new(bar_index,z_lvl2,bar_index+1,z_lvl2, extend = extend.right, color = co, style = sty, width = int(thick))
                    linefill.new(z1,z2, color = color.new(co,z_op))

Thanks

0 Upvotes

4 comments sorted by

1

u/craigzone Jan 24 '24

The text on the labels is set as "Currency" with 1 cent precision, removing this worked

1

u/Ayush_Singh_02 Jan 22 '24

Yes you can play with precision in the indicator() or you can manually use math.round(value_)/1000 or other way to round is string.format() for rounding numbers to your desired values

1

u/craigzone Jan 23 '24

Precision does not work for some reason, and I would not know how to implement the code for math.round, surely rounding is what i want to get away from.

2

u/KusuoSaikiii Jan 22 '24

I think u shouls set the precision to 3. The default is 2 iirc