- Joined
- Oct 25, 2006
- Messages
- 1,550
- Reaction score
- 15
- Points
- 38
Just a little sub routine I whipped together for a need tonight and thought it might be useful for others.
The problem, if you try to do a numeric comparison on a string it will error and crash the macro.
I Could not find anyway in MQ2 to check if a string was a number or not, so I came up with this method for checking:
To use it you would do something like this
Obviously the above should echo "It is not a number", if someString was set to 2 it would have echoed "It is a number". The only condition it will fail on is if you have multiple 0s representing 0 in the string (ex. 00)
The problem, if you try to do a numeric comparison on a string it will error and crash the macro.
I Could not find anyway in MQ2 to check if a string was a number or not, so I came up with this method for checking:
Code:
Sub IsNumber(string numCheck)
/if (!${Defined[numCheck]}) /return FALSE
/if (${numCheck.Equal[0]}) /return TRUE
/declare tInt int local ${numCheck}
/return ${Bool[${tInt}]}
To use it you would do something like this
Code:
/declare someString string local NULL
/varset someString some value
/call IsNumber "${someString}"
/if (${Macro.Return}) {
/echo It is a number
} else {
/echo It is not a number
}
Obviously the above should echo "It is not a number", if someString was set to 2 it would have echoed "It is a number". The only condition it will fail on is if you have multiple 0s representing 0 in the string (ex. 00)