Re: GtkEntry only for numbers
- From: Guy Harris <gharris flashcom net>
- To: Flavio Ribeiro <flavio hackpalace com>
- Cc: gtk-list gnome org
- Subject: Re: GtkEntry only for numbers
- Date: Sat, 3 Mar 2001 17:17:36 -0800
On Wed, Jan 31, 2001 at 03:30:57PM -0200, Flavio Ribeiro wrote:
> b. The easy way: run strtof() on the string and see if it returns 0. If it
> does, the user either entered '0' or entered something illegal. Read
> strtof's man page for more ideas.
% man strtof
No manual entry for strtof
% uname -sr
FreeBSD 3.4-RELEASE
On the other hand:
% man strtod
...
RETURN VALUES
The strtod() function returns the converted value, if any.
If "endptr" is not NULL, a pointer to the character after the
last character used in the conversion is stored in the location
referenced by "endptr".
If no conversion is performed, zero is returned and the value of
"nptr" is stored in the location referenced by "endptr".
If the correct value would cause overflow, plus or minus
HUGE_VAL is returned (according to the sign of the value), and
ERANGE is stored in "errno". If the correct value would cause
underflow, zero is returned and ERANGE is stored in "errno".
...
STANDARDS
The strtod() function conforms to ISO 9899: 1990 (``ISO C'').
so perhaps "strtod()" would be a better alternative, depending on the
platforms on which the application is to be supported (i.e., "strtod()"
is in the version of the C standard supported by most platforms; "strtof()",
as far as I know, isn't, even if it happens to be in GNU libc, or in C99).
Note also that if you hand "strtod()" the string "6.02e23qqqq", it will
return 6.02e+23, not 0.0 - i.e., it doesn't treat extra characters at
the end of the string as an error (presumably because the intent is that
it can be used to carve numeric tokens out of a string).
The way I generally check for errors in "strtol()" is:
char *p;
...
val = strtol(string, &p, base);
if (p == string || *p != '\0')
error;
as that checks either for
1) "strtol()" not liking the input at all (in which case the
pointer to the first character after the number points to the
beginning of the string)
or
2) extra stuff after the number
and, for "strtod()", a similar scheme can be used:
char *p;
val = strtod(string, &p);
if (p == string || *p != '\0')
error;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]