473,624 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9898
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::istringst ream'. 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.co m> <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:
http://www.opengroup.org/onlinepubs/...ns/strtod.html
what is the easy way for only interger?


strtol

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************ *********@u72g2 000cwu.googlegr oups.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
you.

// ----------------------------------------------
// #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<<"Tes t string = "<<TestForNum<< "."<<std::e ndl;
for(size_t i(0); i < TestForNum.size (); ++i){
std::cout<<"cha r at "<<i<<" is "<<TestForNum.a t(i)<<", a ";
if( std::isdigit( TestForNum.at(i ) ) ){
std::istringstr eam strStream( TestForNum.subs tr(i) );
strStream >> Inum;
strStream.clear ();
strStream.str( TestForNum.subs tr(i) );
strStream >> Dnum;
strStream.clear ();
std::cout<<"dig it Inum="<<Inum
<<" Dnum="<<Dnum<<s td::endl;
}
else{ std::cout<<"alp ha"<<std::endl; }
}
std::cout<<"___ ____ isdigit test _______Done"<<s td::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
POVrookie
Mar 9 '06 #4

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

Similar topics

5
13848
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 check if the string contains more than 3 numeric characters because i must only process the ones with more than 3.
4
47550
by: Newsgroups | last post by:
Does anyone know what the VB.NET equivalent to the VB 6.0 String Function is?
3
7888
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.
4
9463
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 coming in from an excel sheet): Examples. 45,666.0041 5664456.12 -5465.25568 ETC
2
1458
by: olskar | last post by:
How can I check the number of values in an array and echo them in my PHP script?
4
3519
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 ID in the query string if it does exist and have them re-enter if incorrect.
4
8364
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 the range, without creating the entire range. for my $i (5 .. 500_005) { push(@results, some_func($i)); }
5
4340
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?
11
12324
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 numbers and add up the totals for each number. on the bill, each line has a few fields,one field containing the phone number, another field containing the number of minutes on that call. the bill is comma delimited.
1
2618
by: Raman Pahwa | last post by:
plz tell me how to check the number of open connections in ASP code.
0
8231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8168
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8614
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7153
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6107
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.