Search code examples
pine-script

Get Decimals places in Pinescript


A forex stock may have five decimals point like EURUSD 1.22189 Another may have two decimal points like BITCOIN/USD like 40102.16

I want the number of digits after the decimal point, i.e. 5 digits in EURUSD and 2 digits in BITCOIN/USD example above.

How do I achieve this in Pinescript?


Solution

  • This will give you what you're looking for.

    //@version=4
    study("Decimals", overlay=true)
    
    var int decimals = int(log10(1/syminfo.mintick))
    
    if barstate.islast
        label.new(bar_index, high, tostring(decimals))
    

    Improved, version 5:

    //@version=5
    study("Decimals", overlay=true)
    
    var int decimals = str.length(str.tostring(syminfo.mintick))-2
    
    if barstate.islast
        label.new(bar_index, high, tostring(decimals))
    

    Another implementation by KryptoNight can be found here.