Validating numeric fields in cobol
For most purposes, this will be sufficient and has the advantages of both speed and clarity.$Bool Result = empty(trim($Test, ' -.,0123456789')); If the $Test contains any character that is not a valid part of a number the remaining string will not be you want to be more restrictive of the ' -' then you can use a separate trim for that and nest it.
ordinal('asdf'); returns false for the exact same reason. signed integers on a 32-bit system (and the same issue on a 64-bit system using 0x7FFFFFFFFFFFFFFF because of two's compliment, anything higher will become a negative number): ordinal(0x7FFFFFFF ); returns 2147483647th (which is correct) ordinal(0x7FFFFFFF 1); returns false.*/ if speed is important, the liberal use of regex should be avoided, especially complex ones like those here.
This was the example code:----- set_locale(LC_NUMERIC,'fr'); is_numeric(12.25); // Return False is_numeric(12,25); // Return True is_float(12.25); //Return False is_float(12,25); //Return True-----This is nonsense!
- no Referring to previous post "Be aware if you use is_numeric() or is_float() after using set_locale(LC_ALL,'lang') or set_locale(LC_NUMERIC,'lang')": This is totally wrong!
To enable a person to use the both the English/American and the rest of the world's way: Other than that, i'd recommend using yours, if it works (i havent tested either yours or mine)By using mine, there might be a slight chance to not being able to do calculations with the numeric string if it's not the English/American way.(*Note: -the E/A way of writing 1 million (with decimal for 1/50): 1,000,000.02-the global way of writing 1 million (with decimal for 1/50): 1.000.000,02/* Example Outputs of ordinal(): 0th, 1st, 2nd, 3rd, 4th, ..., 9th, 10th, 11th, 12th, 13th, 14th, ..., 19th, 110th, 111th, ..., 199th, 200th, 201st.
Depending upon your specific application you can also choose to ignore spaces and allow for 'e' notation.
Keep in mind that some notations pt the sign to the right of the number instead of to the left.
Here's an even simpler pair of functions for finding out if a number is odd or even:function IS_ODD($number) function IS_EVEN($number) Test:$my Number = 151;if(IS_ODD($my Number)) echo("number is odd\n");else echo("number is NOT odd\n");if(IS_even($my Number)) echo("number is even\n");else echo("number is NOT even\n"); Results:number is oddnumber is NOT even Output:11111111111111111:string is Integer? is_numeric('0123') returns true but 0123 without quotes cannot be inserted into SQL.
According to wiki.hashphp.org, "Many people reach for is_numeric() thinking this is a good approach, and it will seem to work just fine.