EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2016
11:57 AM
strtoi() = invalid?
i tried using StrToI and noticed it does not behave as advertised:
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)?
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 5

Komag
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2016
08:26 PM
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 😄


Roku Employee
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2016
11:28 AM
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.
EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2016
09:27 AM
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...")
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...")
belltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2016
10:03 AM
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
EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2016
10:52 AM
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")