Talk:Determine if a string is numeric

From Rosetta Code
Revision as of 22:52, 11 December 2008 by rosettacode>ShinTakezou (→‎Subtle bug in the C example?: C example return true if void string is passed (?))

Subtle bug in the C example?

Carefully reading the strtol() man page on my Linux box I see that the entire resulting string was converted to a long if (and only if) *endptr (p in the example) is '\0' (an ASCII NUL) and if the *nptr (s in the example) was NOT '\0'. In other words I think the example should test for a precondition to return false if it's called with an empty string as the argument. If I were more confident in my C coding skills and my understanding of this particular function (and of the conformance of Linux to any relevant standards) I would insert a line like: if *s == '\0' return !*s;

Am I mistaken?

JimD 16:16, 11 October 2007 (MDT)

I think you are right: if you pass a void string, it returns true ... But it could be interpreted like: a void string can rapresent anything... :D I rather would add

<c>if ( s==NULL ) return 0; if ( *s == 0 ) return 0;</c>

This checks also for NULL pointer passed (odd!). --ShinTakezou 22:52, 11 December 2008 (UTC)

Unix shell

#!/bin/sh

a='123'
if [ "$a" -eq "$a" ] 2>/dev/null
then
    echo "a is numeric"
else
    echo "a is abnumeric"
fi

Exact definition of IsNumeric?

For those who don't know VB: How exactly is IsNumeric defined? For example: Is leading/trailing whitespace allowed (i.e. " 123" or "123 ")? Does it also accept floting point values (e.g. "2.5" or "1e5")? What about thousands separators (e.g. "10,000")? Is that locale-dependent? Are numbers in other bases (e.g. hexadecimal) allowed (assuming VB supports them otherwise)? What about numbers too big to fit into a native integer (e.g. "9999999999999999999999999999999999999999999999999") resp. a native float (e.g. "1e1234567")?

And what is its input? The Python and C samples take a string. The Ruby and Scheme samples take an object. And in Tcl it's the same thing.
Half the samples shown implement 'is integer', or 'is numeric character'.
In VB, IsNumeric is a function for validating input. VB examples (all true)
1, 1.1, -1.1, "1", "1.1", "-1.1" "1.1-", " 1.1- ", " 1,1 ", " 1E1" "&HFF"
VBscript:
msgbox isnumeric(1)
msgbox isnumeric(1.1)
msgbox isnumeric(-1.1)
msgbox isnumeric("1")
msgbox isnumeric( "1.1")
msgbox isnumeric( "-1.1")
msgbox isnumeric( " 1.1- ")
msgbox isnumeric(" 1,1 ")
msgbox isnumeric(" 1E1 ")
msgbox isnumeric(" &HFF ")

I hadn't realized that there was an ambiguity. I hadn't even realized that "isnumeric" is a VB function (I certainly don'ty know VB). In the two examples I contributed (IDL and TCL) I assumed that the task meant that something would be interpreted as a number if handed to the language in question. I.e. if I can multiply it with two or take the sin() of it then it is numeric. For example in IDL I might say "sin(double(x))" where "double(x)" converts the input into a "double" (8-byte float) which will fail if "x" is, for example, the string "foo". I trap the error and decide what is or isn't "numeric" based on the occurrence of this kind of error. This will allow "1.1" or "-.1e-04" or "+000003" etc.
Should we tag the task for clarification? Sgeier 10:34, 20 September 2007 (MDT)

Objective-C question

An anonymous user had posted a question about the Objective-C example. I'll try to translate.

"How to check the whole string to make sure it is numeric?" was the original question. I think they were looking for a character by character check? Maybe a regex? What do you Ob-C people think? --Mwn3d 21:46, 11 December 2008 (UTC)