472,794 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 software developers and data experts.

the easy way to check if the string is a number or not?

I want to know what is the easy way to check if a string is a number or
not? the number can be int, float, double, scientific,...

what is the easy way for only interger?

Mar 8 '06 #1
3 9829
kathy wrote:
I want to know what is the easy way to check if a string is a number or
not? the number can be int, float, double, scientific,...

Try to read it using a 'std::istringstream'. If this is not what you
want, check out Boost's regular expression library and provide a
regular expression describing the formats you want.
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 8 '06 #2
On 8 Mar 2006 13:34:46 -0800, "kathy" <yq*****@yahoo.com> wrote:
I want to know what is the easy way to check if a string is a number or
not? the number can be int, float, double, scientific,...
check out strtod:
what is the easy way for only interger?


In both cases you need to understand what the 'endptr' does. Hint:
strip trailing whitespace before calling strtod or strtol. Encapsulate
the check in a reusable function.

Best wishes,
Roland Pibinger
Mar 8 '06 #3

kathy wrote in message
<11*********************@u72g2000cwu.googlegroups. com>...
I want to know what is the easy way to check if a string is a number or
not? the number can be int, float, double, scientific,...
what is the easy way for only interger?

If the others did not give you the answer you wanted, maybe this will help

// ----------------------------------------------
// #include <ccytpe> // ISO C++ 14882: std::isdigit()
std::cout<<"\n_______ isdigit test _______"<<std::endl;
int Inum(0);
double Dnum(0);
std::string TestForNum("Hi 4 76.5num 42.7e-5");
std::cout<<"Test string = "<<TestForNum<<"."<<std::endl;
for(size_t i(0); i < TestForNum.size(); ++i){
std::cout<<"char at "<<i<<" is "<<TestForNum.at(i)<<", a ";
if( std::isdigit( TestForNum.at(i) ) ){
std::istringstream strStream( TestForNum.substr(i) );
strStream >> Inum;
strStream.str( TestForNum.substr(i) );
strStream >> Dnum;
std::cout<<"digit Inum="<<Inum
<<" Dnum="<<Dnum<<std::endl;
else{ std::cout<<"alpha"<<std::endl;}
std::cout<<"_______ isdigit test _______Done"<<std::endl;
/* -- output --
_______ isdigit test _______
Test string = Hi 4 76.5num 42.7e-5.
char at 0 is H, a alpha
char at 1 is i, a alpha
char at 2 is , a alpha
char at 3 is 4, a digit Inum=4 Dnum=4
char at 4 is , a alpha
char at 5 is 7, a digit Inum=76 Dnum=76.5
char at 6 is 6, a digit Inum=6 Dnum=6.5
char at 7 is ., a alpha
char at 8 is 5, a digit Inum=5 Dnum=5
char at 9 is n, a alpha
char at 10 is u, a alpha
char at 11 is m, a alpha
char at 12 is , a alpha
char at 13 is 4, a digit Inum=42 Dnum=0.000427
char at 14 is 2, a digit Inum=2 Dnum=2.7e-005
char at 15 is ., a alpha
char at 16 is 7, a digit Inum=7 Dnum=7e-005
char at 17 is e, a alpha
char at 18 is -, a alpha
char at 19 is 5, a digit Inum=5 Dnum=5
_______ isdigit test _______Done
// ----------------------------------------------

Open <ccytpe> in an editor for a list of more useful items.

Bob R
Mar 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: ief | last post by:
hi all, i'm trying to check the length of a numeric value in a string. this is what i need to do: I have a string "Mystring (253)" and a string "SecondString (31548745754)" Now i have to...
by: Newsgroups | last post by:
Does anyone know what the VB.NET equivalent to the VB 6.0 String Function is?
by: Water Cooler v2 | last post by:
What is the .NET equivalant of the VB function String(Number as Long, Character)? I am forgetting it at the moment.
by: Phil Mc | last post by:
OK this should be bread and butter, easy to do, but I seem to be going around in circles and not getting any answer to achieving this simple task. I have numbers in string format (they are...
by: olskar | last post by:
How can I check the number of values in an array and echo them in my PHP script?
by: JBiggsCC | last post by:
I have a very simple login page which takes an ID number via a HTML form GET. What is easiest way to check that ID number against an Access DB to see if it exists? I want to redirect with the...
by: Aaryan123 | last post by:
Function to check a number between a range in perl ========================================= Back ground: This situation has been fixed in Perl5.005. Use of .. in a for loop will iterate over...
by: Cylix | last post by:
Is there any existing method in VB.NET or any 3-third party function can find out the language in a string? Let say, isChinese? isFrench?
by: davidj411 | last post by:
i am parsing a cell phone bill to get a list of all numbers and the total talktime spend on each number. i already have a unique list of the phone numbers. now i must go through the list of...
by: Raman Pahwa | last post by:
plz tell me how to check the number of open connections in ASP code.
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.