Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Converting String to Long or Float


past12 Nov 18, 2014 04:01 PM

I have the need to cast a string (contained into an array of strings) into a Long, or a Float. I've already tried to assign the string to a Long variable, but with no luck.

In general, what I need to do is:

Public results(7) as String*100
Public policy as Long
...
policy = results(2) 'e.g. results(2) contains the string 12
policy = policy+30 'this sum must return 42, now returns 1230 (string concatenation)

I've found functions to convert from number to string, but not vice-versa.


JDavis Nov 18, 2014 05:03 PM

The following code is working correctly for me:

Public results(7) As String * 100
Public policy As Long

BeginProg
results(2) = "12"
Scan (1,Sec,3,0)
policy = results(2) 'e.g. results(2) contains the string 12
policy = policy+30 'this sum must return 42

NextScan
EndProg


When you do Variable1 = Variable2 in CRBasic, it does an implicit type cast. A string should always be cast into a Long or Float before you try adding a number to it. The '+' operator has odd behavior in some cases when used on strings.


past12 Nov 24, 2014 03:31 PM

Ok, that works for me.
Sorry, my code was a little different, actually. I summed 30 "inline", sort of

policy = results(2)+30

which actually concateneted the string "30" to results(2)

Log in or register to post/reply in the forum.