Sparky Posted September 7, 2007 Report Share Posted September 7, 2007 Does anyone know of a string VI that checks a string to see if it is a valid decimal number before sending the string to a convert VI? The string could have one of 4 possibilities for the first character (+, -, Number, Decimal Pt.), and can then have either numbers or a decimal pt. after. It must not have more than one decimal Pt., or Plus/Minus sign. Just thought I would check before I start doing my own. I am using LV 8.2. It would be nice if the string to number conversions had some error output. Thanks, Sparky Quote Link to comment
Justin Goeres Posted September 7, 2007 Report Share Posted September 7, 2007 QUOTE(Sparky @ Sep 6 2007, 09:46 AM) Does anyone know of a string VI that checks a string to see if it is a valid decimal number before sending the string to a convert VI? The string could have one of 4 possibilities for the first character (+, -, Number, Decimal Pt.), and can then have either numbers or a decimal pt. after. It must not have more than one decimal Pt., or Plus/Minus sign. Just thought I would check before I start doing my own. I am using LV 8.2. It would be nice if the string to number conversions had some error output. This sounds like a job for http://www.regular-expressions.info/' target="_blank">Regular Expressions! Quote Link to comment
Sparky Posted September 7, 2007 Author Report Share Posted September 7, 2007 QUOTE(Justin Goeres @ Sep 6 2007, 12:57 PM) This sounds like a job for http://www.regular-expressions.info/' target="_blank">Regular Expressions! I have not used the 'regular expressions' VI. It looks like that might do the trick. Thanks you very much! Sparky Quote Link to comment
orko Posted September 7, 2007 Report Share Posted September 7, 2007 Hey Sparky, Check this out: <labview>\examples\general\strings.llb\Extract Numbers.vi The code in the while loop is probably all you need (Note that they don't match for a "+" sign at the front of the number). Quote Link to comment
Justin Goeres Posted September 7, 2007 Report Share Posted September 7, 2007 QUOTE(Sparky @ Sep 6 2007, 10:25 AM) I have not used the 'regular expressions' VI. It looks like that might do the trick. Thanks you very much! There are actually a couple ways to do this. You can use the Match Pattern function, or you could do a full-blown PCRE with the Regular Expression function (that I love so very much). In this case, a Match Pattern is probably good enough. I think this snippet gets you most of what you want: http://forums.lavag.org/index.php?act=attach&type=post&id=6869 To break it down... ^ anchor the match to the beginning of the string (require the number to occur at the front of the string) [+-]? match the '+' or '-' character 0 or 1 times (so strings without a + or - still match) [0-9]* match any digits 0 or more times (so strings that don't have digits in front of the decimal still match) \.? match the literal decimal point ('.') 0 or 1 times (so strings that don't have the decimal at all still match) [0-9]* match any digits 0 or more times (so strings that don't have digits after the decimal still match) $ anchor the match to the end of the string (so any extra characters beyond the number cause a failed match) Now, it turns out this example is not without its problems. For instance, it will still match: An empty string (change the >=0 to a strict >0 to fix that) Various non-numeric strings like "+" or "." or "+." The second item is kind of tough to deal with, although you could run it through a pre-validation step with a regex like [0-9]? to make sure the string contains at least one digit. You could also probably handle it with a cleverly constructed regex and the full-blown Regular Expression function, but that's left as an exercise to the reader . Quote Link to comment
wevanarsdale Posted September 7, 2007 Report Share Posted September 7, 2007 QUOTE(Sparky @ Sep 6 2007, 12:46 PM) Does anyone know of a string VI that checks a string to see if it is a valid decimal number before sending the string to a convert VI? The string could have one of 4 possibilities for the first character (+, -, Number, Decimal Pt.), and can then have either numbers or a decimal pt. after. It must not have more than one decimal Pt., or Plus/Minus sign. Just thought I would check before I start doing my own. I am using LV 8.2. It would be nice if the string to number conversions had some error output. Sparky: I'd use the Fract/Exp String to Number function to convert a string or array of strings. This conversion is not sensitive to characters subsequent to the numeric part. You can set the default value to NaN (assuming this value does not appear in your data) and test for a conversion error. You can convert back using the Number to Fractional String or similar function to remove any trailing non-numeric characters. This approach is useful for reformatting numeric string arrays. http://forums.lavag.org/index.php?act=attach&type=post&id=6868''>http://forums.lavag.org/index.php?act=attach&type=post&id=6868'>http://forums.lavag.org/index.php?act=attach&type=post&id=6868 Quote Link to comment
Justin Goeres Posted September 7, 2007 Report Share Posted September 7, 2007 QUOTE(orko @ Sep 6 2007, 10:40 AM) <labview>\examples\general\strings.llb\Extract Numbers.vi(Note that they don't match for a "+" sign at the front of the number). That example will also incorrectly match the string ".0.3" Quote Link to comment
orko Posted September 7, 2007 Report Share Posted September 7, 2007 QUOTE(Justin Goeres @ Sep 6 2007, 11:02 AM) That example will also incorrectly match the string ".0.3" Hehe... what's wrong with that? Dunno, I can't think of a reason they were looking for an initial period at the front of the number... ah well, so much for the examples Quote Link to comment
Sparky Posted September 7, 2007 Author Report Share Posted September 7, 2007 QUOTE(orko @ Sep 6 2007, 02:13 PM) Hehe... what's wrong with that? Dunno, I can't think of a reason they were looking for an initial period at the front of the number... ah well, so much for the examples Could have a decimal point as the first char. if the operator enters .985 for example instead of 0.985. I don't think I can guarantee that the operator will not just enter the decimal point first. I guess I could force a zero as the first char if the operator just entered a decimal point. I would prefer to allow him/her to do it either way though. Thanks for all the good comments. Sparky QUOTE(wevanarsdale @ Sep 6 2007, 01:51 PM) Sparky: I'd use the Fract/Exp String to Number function to convert a string or array of strings. This conversion is not sensitive to characters subsequent to the numeric part. You can set the default value to NaN (assuming this value does not appear in your data) and test for a conversion error. You can convert back using the Number to Fractional String or similar function to remove any trailing non-numeric characters. This approach is useful for reformatting numeric string arrays. http://forums.lavag.org/index.php?act=attach&type=post&id=6868 I think the "Fract/Exp String to Number needs the leading zero, and won't work if the first char. is just a decimal point. Quote Link to comment
wevanarsdale Posted September 8, 2007 Report Share Posted September 8, 2007 QUOTE(Sparky @ Sep 6 2007, 02:51 PM) I think the "Fract/Exp String to Number needs the leading zero, and won't work if the first char. is just a decimal point. The Fract/Exp String to Number function correctly recognizes a leading decimal point for all versions of LabVIEW (7.0, 7.1, 8.2, 8.5) currently on my office computer. Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.