NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Converting a string to an int (returning null, not zero)

Solved!
Go to solution

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).

0 Kudos
Message 1 of 4
(2,440 Views)
Solution
Accepted by topic author BIOSphere

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

Message 2 of 4
(2,425 Views)

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.

0 Kudos
Message 3 of 4
(2,404 Views)

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

0 Kudos
Message 4 of 4
(2,393 Views)