04-17-2019 11:03 AM
I need to convert a string to an int, however, if the value does not convert directly to an int, I would like it to return null, rather than 0 (which Val() returns).
Solved! Go to Solution.
04-17-2019 12:00 PM
Hi,
You can use the following expression to convert a String into an Int:
Locals.Number = Str(Val(Locals.String),"%d") == Trim(Locals.String) ? Val(Locals.String) : NAN
If Locals.String contains an integer then it will assign the value to Locals.Number, otherwise it will assign NAN.
I hope this helps,
Charlie Rodway | Principal Software Engineer | Certified TestStand Architect (CTA)
Computer Controlled Solutions Ltd | NI Silver Alliance Partner | GDevCon#1 Sponsor
04-18-2019 10:29 AM - edited 04-18-2019 10:43 AM
Thank you!
What if I need that number to be in decimal format (ie. 3.270)?
I noticed that when I type a decimal in, it will not format it correctly into an int.
04-19-2019 02:35 AM
Hi,
If you need it to work for decimal numbers, rather than integers (whole numbers), use the following expression:
Locals.Number = Str(Val(Locals.String),"%g") == Trim(Locals.String) ? Val(Locals.String) : NAN
Kind regards,
Charlie Rodway | Principal Software Engineer | Certified TestStand Architect (CTA)
Computer Controlled Solutions Ltd | NI Silver Alliance Partner | GDevCon#1 Sponsor