string2float v1.0
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © CA_Lobo

//@version=5
indicator("string to float")

str2float(xstr) => 
    temp = str.split(xstr,".")
    integer = str.split(array.get(temp,0),"")
    fraction = array.size(temp) == 2 ? str.split(array.get(temp,1),"") : array.new_string()
    // reverse for easier parsing... must be on own line rather than embedded within the ternary operator
    // as will generate a cannot call operator ?: with argument 'expr1'='call 'array.reverse' (void)' error.
    array.reverse(fraction)
    
    power = 1                                   // working to the left of the decimal point so use integer
    rtnValue = 0.0                              // make it a float... if you need an integer cast it afterwards...
    sign = 1                                    // assume it's positive unless we find a minus sign
    
    while array.size(integer) > 0
        digit = array.pop(integer)
        switch digit
            "1" => rtnValue += (1 * power)
            "2" => rtnValue += (2 * power)
            "3" => rtnValue += (3 * power)
            "4" => rtnValue += (4 * power)
            "5" => rtnValue += (5 * power)
            "6" => rtnValue += (6 * power)
            "7" => rtnValue += (7 * power)
            "8" => rtnValue += (8 * power)
            "9" => rtnValue += (9 * power)
            "-" => sign := -1                   // use to make it a negative number (multiplying by -1 doesn't work with a zero rtnValue eg -0.125)
            =>  na                              // everything else is inrelevant...
        power *= 10                             // advance to next LARGER power
    
    power2 = 0.1                                // working to the right of the decimal point, need float otherwise pinescript complains...
    while array.size(fraction) > 0
        digit = array.pop(fraction)
        switch digit
            "1" => rtnValue += (1 * power2)
            "2" => rtnValue += (2 * power2)
            "3" => rtnValue += (3 * power2)
            "4" => rtnValue += (4 * power2)
            "5" => rtnValue += (5 * power2)
            "6" => rtnValue += (6 * power2)
            "7" => rtnValue += (7 * power2)
            "8" => rtnValue += (8 * power2)
            "9" => rtnValue += (9 * power2)
            =>  na                              // everything else is inrelevant
        power2 /= 10                            // move to the next SMALLER power
    
    rtnValue *= sign                            // now make the entire number negative if we had a minus sign in the integer portion, 
    rtnValue                                    // and return this value


printDebug(txt) => var table t = table.new(position.middle_right, 1, 1), table.cell(t, 0, 0, txt, bgcolor = color.yellow)

// test cases
var test = str.split('4235,-12134,4236.0,-1568.32, 0.125, -0.256, 9876543210.123456789, -9876543210.123456789',",")
testFloats = array.new_float()

for i = 0 to array.size(test) - 1
    //array.push(testFloats, str2float(array.get(test,i)))
    array.push(testFloats, str.tonumber(array.get(test,i)))

printDebug(str.tostring(testFloats))
