ehwalker Posted July 10, 2018 Posted July 10, 2018 I found a thread regarding this from 11 years ago, but couldn't find anything else so thought it may be best to check if the newer versions of FM have a solution to this problem. Is there any function or custom function that would allow me to check if the value in a field is numeric? I have a field that is supposed to be only numeric (primarily only positive integers) however occasionally there are entries like '1 + 4' or '10 + 2'. I'd like to enter the contents of this field into another field, but only with the entries that are numeric only. Any thoughts? Really appreciate any help.
rwoods Posted July 10, 2018 Posted July 10, 2018 (edited) Hi Eli I know someone will have a better answer than this, but my thoughts would be 1) Make the field type be 'number' and use the fields options validation set to 'Strict Data Type = Numeric' 2) If the field has to be a text field, but you want to check that it is numeric, the you could do something like :- FieldIsNumber = If ( Filter ( numberField ; "0123456789" ) = numberField ; 1 ; 0 ) This Boolean result would be 1 if the field only contained numerals. It would give a positive result for '01234', but this may be OK. Edited July 10, 2018 by rwoods 1
NLR Posted July 10, 2018 Posted July 10, 2018 (edited) I'm assuming the field you refer to is defined as a text field. The test below is based on this. A test for a TEXT field's contents being purely numeric is: GetAsNumber ( Field) = Field An example: If a text field contains the value: 1+4 GetAsNumber (1+4) = 14 returns false, therefore the value '1+4' would be excluded for you. On the other hand, GetAsNumber (18) = 18 returns true, therefore the value '18' would be accepted. But if your field is defined as a number field, a more stringent test needs to be used such as: GetAsNumber ( Field) = Field and Length(GetAsNumber ( Field)) = Length(Field) Ralph Edited July 10, 2018 by NLR 1
Lee Smith Posted July 10, 2018 Posted July 10, 2018 You might also try the Filter Funtion Filter ( yourField ; "0123456789" ) 1
ehwalker Posted July 11, 2018 Author Posted July 11, 2018 Thanks everyone for all the suggestions, these are all very helpful and have solved my problem. Really appreciate it!
OlgerDiekstra Posted July 11, 2018 Posted July 11, 2018 On 7/10/2018 at 6:18 PM, rwoods said: Hi Eli I know someone will have a better answer than this, but my thoughts would be 1) Make the field type be 'number' and use the fields options validation set to 'Strict Data Type = Numeric' 2) If the field has to be a text field, but you want to check that it is numeric, the you could do something like :- FieldIsNumber = If ( Filter ( numberField ; "0123456789" ) = numberField ; 1 ; 0 ) This Boolean result would be 1 if the field only contained numerals. It would give a positive result for '01234', but this may be OK. FieldIsNumber would return false for 1.0 (or 1,5 depending on your language) so make sure you include the dot and/or comma as well in the filter.
rwoods Posted July 11, 2018 Posted July 11, 2018 Good point @OlgerDiekstra It certainly needs the decimal delimiter in the Filter list!
Recommended Posts
This topic is 2346 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now