Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
EnTerr
Level 11

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)? 
0 Kudos
5 REPLIES 5
Komag
Level 10

Re: strtoi() = invalid?

"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 😄
0 Kudos
RokuKC
Roku Employee
Roku Employee

Re: strtoi() = invalid?

"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.
0 Kudos
EnTerr
Level 11

Re: strtoi() = invalid?

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...")
0 Kudos
belltown
Level 9

Re: strtoi() = invalid?

"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
https://github.com/belltown/
0 Kudos
EnTerr
Level 11

Re: strtoi() = invalid?

"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")
0 Kudos