PDA

View Full Version : IsNum? - Possible to check if a string is a number?


AdamEisfeld
10-26-2010, 01:55 AM
I know in various other languages its possible to call a function such as isNum on a string that will return a 1 if the given string is a number, otherwise 0 if the string contains letters and/or symbols.

I cant seem to find a similar command in MEL. Ive searched the forums and through the documentation with no luck. Im aware its possible to program this command on my own via breaking the string apart and checking it manually, but if an internal command already exists Id prefer to go that route.

Alternatively --

Is it possible to restrict a textField's input to only numbers? Again, I cant seem to find any flags or info on this in the documentation / forums. Perhaps Im looking in the wrong place, but it seems something relatively simple so Im surprised I havent found anything on it.

Essentially Im in the final stages of cleaning up my code and attempting to block any user-made errors, and obviously having the user mistakenly enter a letter into a text box that requires it's text to be used in a mathematical calculation isn't a good idea. Therefor either restricting input to only numbers or checking at a later time if the user has mistakenly entered letters / symbols and alerting them of their error would be a cleaner end result.

Any help? Thanks in advance,
- Adam Eisfeld

AdamEisfeld
10-26-2010, 02:14 AM
Ah, well this is embarassing. I just realized there are other types of fields that can be created, including a floatField. Exactly what I needed, sorry for the pointless post.

To anyone else wondering, instead of using a textField control, use a floatField control to restrict the input to decimal numbers only, or alternatively an intField control to restrict the input to non-decimal numbers only.

Sorry again!
- Adam Eisfeld

NaughtyNathan
10-26-2010, 09:19 AM
as a side note, in MEL you can also simply cast variables to a different type (within reason):

string $text = "3.14";
float $pi = $text;
print ($pi * 2);

print (float($text) * 2); // inline casting

:nathaN

ewerybody
10-26-2010, 01:23 PM
another sidenode: I mean if you really come across this problem:
You could do something like this:{
string $value = "345.343123";
if ($value == `match "[0-9.]+" $value`)
print("$value: \"" + $value + "\" is numbers only!\n");
else
print("$value: \"" + $value + "\" is NOT numbers only!\n");
}

AdamEisfeld
10-26-2010, 02:26 PM
Nathan I know that you can simply cast a string variable into a float or integer but the issue was determining if there is a letter in this string variable before casting, as this would give undesired results if you tried to cast "0.4ab" into a float. Or am I mistaken and MEL will actually strip the letters during casting? I was under the assumption that MEL would try to grab the binary or hex equivelant of the letters and add them to the number during conversion.

ewerybody thanks I havent tried your method but if that is how the match procedure works then cool, Ill keep it in mind in case I run into a situation later that needs it.

NaughtyNathan
10-26-2010, 04:15 PM
nah, if it's not a valid number Maya converts it to 0... which is pretty much what you'd want to happen I guess, why bother trying to figure out what the user means? if they don't type a real value they get zero. But it's all academic, using the correct control for the job is the only real solution! ;)

:nathaN

ewerybody
10-26-2010, 06:03 PM
btw:print ((float)"0.4ab");works!:D

CGTalk Moderation
10-26-2010, 06:03 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.