String Parsing

Find and share HowTos to various installations / configurations!
3 posts • Page 1 of 1
adambutt
Posts:6
Joined: Wed May 20, 2015 6:05 pm

String Parsing

Post by adambutt »

I am working on my first project with OA and I am still in the process of getting familiar with the system. Also, If my question is inappropriate for this forum, please let me know.

As part of my project, I am trying to provide an interface to my users to allow them to modify setpoints which have a float datatype. However, I don't see any provisions to parse string as a float other than simply casting it. Specifically, I would like to perform a tryParse() so that I can catch invalid strings and alert the user. However, using the cast method simply returns a 0.0 value on invalid strings and I can not differentiate between an invalid string or a legitimate "0.0" value.

Please let me know if you have any suggestions. Thank you.

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: String Parsing

Post by mkoller »

Better use a text input field with the definition of the input just being allowed to be a float, so that the user simply can not enter invalid data.
In gedi create a textfield (single line text input object), in property editor select "Text Format" and choose Float.
To convert the string the user entered, you can just do an assignment
float f = this.text
An alternative, in general, to read a float from a string, use sscanf(), e.g. sscanf(str, "%3.2f", f);

adambutt
Posts:6
Joined: Wed May 20, 2015 6:05 pm

Re: String Parsing

Post by adambutt »

Ahhh. I noticed that some of the existing entry fields had that property and I was wondering how that was done. Thank you.

3 posts • Page 1 of 1