Forum Discussion

EnTerr's avatar
EnTerr
Roku Guru
9 years ago

strtoi() = invalid?

i tried using StrToI and noticed it does not behave as advertised:
StrToI(str as String) as Dynamic
Return the integer value of the string, or invalid if str is not a string.

I can see the intent was to return invalid if the string does not parse well, since the return type is Dynamic and not Integer. But nowadays it returns 0 instead, doc and behavior are out-of-sync.

Also, when speaking of conversion string -> int, what are the differences between strtoi(str), string.toint() and val(str, 10)? 

5 Replies

  • "EnTerr" wrote:
    Also, when speaking of conversion string -> int, what are the differences between strtoi(str), string.toint() and val(str, 10)? 

    I second this question 😄
  • RokuKC's avatar
    RokuKC
    Roku Employee
    "EnTerr" wrote:
    i tried using StrToI and noticed it does not behave as advertised:


    The documentation is incorrect. Thanks for pointing it out.

    The functions you mention are all essentially equivalent.
  • Thank you... a follow-up question: how am i supposed to determine if a string is actually a proper number?
    Having the `invalid` flag would have covered it. Lacking that, such check seems to shard into a fractal of special cases ("so, i got 0 back? maybe it was a real 0 - or maybe it wasn't; maybe it has whitespaces, plus/minus in front - or maybe it starts with digit but ends with letters...")
  • "EnTerr" wrote:
    how am i supposed to determine if a string is actually a proper number?

    Define what you mean by a 'proper number' and use a regular expression to check for that:


    function isProperNumber(s as dynamic) as boolean
       isProper = false
       if GetInterface(s, "ifString") <> invalid
           re = CreateObject("roRegex", "^\s*[+-]?\d+$", "")
           if re.IsMatch(s)
               isProper = true
           end if
       end if
       return isProper
    end function
  • "belltown" wrote:
    "EnTerr" wrote:
    how am i supposed to determine if a string is actually a proper number?

    Define what you mean by a 'proper number' and use a regular expression to check for that

    Exactly my point! I don't want to define myself what a proper number format is - i want to delegate that to Roku's implementation. Would not like to play catch-up with parser changes, that's an error-prone chase game - it's not like the Co will spec out what a "number" is to them. Fancy a guess what the below outcome is?
    Brightscript Debugger> ? val("0x123456")