Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-18-2005, 06:58 AM   #1 (permalink)
keeling
Registered User
 
Join Date: Mar 2005
Posts: 4
keeling is on a distinguished road
[C]IsNumeric: Will this work?

Please forgive me if this is a 'dumb' question, but I am not much of a C coder.

I am working on some routines to be used in a different language. One of the routines I want is a check to see if a string is numeric (like the VB command). This is what I have come up with:

Code:
int IsNumeric(char *pString){
	char *pEnd;
	double dbl;
	dbl = strtod (pString,&pEnd);
	if (pString != pEnd && *pEnd == '\0'){
		//it is numeric
		return 0;
	}
	else{
		//it isn't
		return -1;
	}

}
I have tried a few tests with it and it seems to work, but I am great for over looking obvious.

Thanks for helping a bad c coder.

Last edited by Valmont; 03-18-2005 at 08:40 AM.
keeling is offline   Reply With Quote
Old 03-18-2005, 11:53 AM   #2 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 734
DJMaze is on a distinguished road
strtod() converts a string to a double or long double value and can handle numbers like
+ 1231.1981 e-1
502.85E2
+ 2010.952
strtod stops reading the string at the first character that cannot be interpreted as an appropriate part of a double value.

Your pEnd must be '\0' else it could still fail.
The (pString != pEnd) is also TRUE on values like "502.85E2 not number"

atoi()
Is another way to check numberic values but then only with integers.
DJMaze is offline   Reply With Quote
Old 03-18-2005, 03:03 PM   #3 (permalink)
keeling
Registered User
 
Join Date: Mar 2005
Posts: 4
keeling is on a distinguished road
OK, maybe I'm missing something... but isn't that what I have.

With the example you gave ...."502.85E2 not number" ... I got it to return false because it isn't a number.

Perhaps I have miss understood what you were saying. As I said, please forgive me for being a bit slow. C is not a native nor friendly tongue for me.

All I'm trying to do is make sure a string I pass to this routine is numeric. I don't care if its short or double (hence why I used strtod). I know that strtod will take leading and trailing whitespace, but I can handle that.

Thanks
keeling is offline   Reply With Quote
Old 03-19-2005, 03:56 AM   #4 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 734
DJMaze is on a distinguished road
pEnd points to the first character which doesn't belong to the numeric.
Code:
char *pString = "502.85E2 not number";
char *pEnd;
double dbl = strtod (pString,&pEnd);
printf("The string is '%s' the number is '%lf'\n", pEnd, dbl);
This should output: The string is ' not number' the number is '502.85E2'
DJMaze is offline   Reply With Quote
Old 03-19-2005, 09:55 AM   #5 (permalink)
keeling
Registered User
 
Join Date: Mar 2005
Posts: 4
keeling is on a distinguished road
But what I want to check is if the WHOLE string is numeric. I don't what to 'get' the numeric part.

So again, with the "502.85E2 not number" the routine SHOULD return false because it is not numeric.

Thanks again mate
keeling is offline   Reply With Quote
Old 03-19-2005, 10:11 AM   #6 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 734
DJMaze is on a distinguished road
It was just a small explain of your correct code.
Here's a smaller version
Code:
bool IsNumeric(char *pString){
	char *pEnd;
	double dbl = strtod (pString,&pEnd);
	return (pString != pEnd && *pEnd == '\0');
}
DJMaze is offline   Reply With Quote
Old 03-19-2005, 11:13 AM   #7 (permalink)
keeling
Registered User
 
Join Date: Mar 2005
Posts: 4
keeling is on a distinguished road
Ok, thanks.

I have to use the int type (as opposed to bool) because the language I am writing for (FreeBasic) doesn't support boolean.

Thanks again
keeling is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Articels/snippets on the left dont work Apodysophilia Feedback 2 02-08-2005 07:05 PM
Do Multidimensional arrays work like a hash in Perl? philthee Java 1 10-22-2004 02:06 PM
This should work so why doesn't it ntws01 Standard C, C++ 18 08-27-2003 02:24 PM
San Diego Tech. work. Admin Lounge 10 02-03-2003 07:19 PM
Getting X to work in Debian w00t Linux / BSD / OS X 7 08-25-2002 02:44 PM


All times are GMT -8. The time now is 08:46 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting