473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

C#: whether string is number or not

750 Expert 512MB
i had come across this kinda puzzle... there a function which takes in a input in form of string.. the string can be as long as required.. (may cause overflow exception).. its required to test whether the string is numeric or not...numeric includes int,long, currency. i first thought of regular expression... or at the basic maybe try parsing to right type.... Even if you leave out currency... is there a way of doin this? is there something in .net that i m missing?

Expand|Select|Wrap|Line Numbers
  1. private bool isNumber(string s)
  2.         {
  3.             bool num = true;
  4.             try
  5.             {
  6.                 // Code to test whether s can be a number (integer, double or maybe currency)
  7.                 // the first way is int.Parse Int64.Parse(s); or regular expression
  8.  
  9.             }
  10.             catch (Exception e)            
  11.             {
  12.                 num = false;
  13.             }
  14.  
  15.  
  16.             return num;
  17.  
  18.  
  19.         }
  20.  
Sep 10 '08 #1
6 5716
pootle
68
Hi,

No, you are not missing something. Shameful isn't it? You would expect this kind of simple operation to be included somewhere in such a rich framework...

You have both of the common ways to solve this problem. Just for completeness, the regular expression approach (my personal preference) can be done using the following:


Expand|Select|Wrap|Line Numbers
  1. private static Regex _isNumber = new Regex(@"^\d+$");
  2. public static bool IsNumeric(string inputData) {
  3.    Match m = _isNumber.Match(inputData);
  4.    return m.Success;
  5. }
You could also make incremental char comparisons within a for loop testing if each char of the string is a number.

Regards.
Sep 10 '08 #2
PRR
750 Expert 512MB
ya i would too go with regular expression.... i had also written a function for conversion... (again a long string)... i thought maybe there's a shortcut way of doing it...thx for ur reply ...
Sep 10 '08 #3
tlhintoq
3,525 Expert 2GB
try
{
int x = Convert.ToInt64(stringVariable);
Catch
{
// then it isn't a number
}
Sep 11 '08 #4
PRR
750 Expert 512MB
try
{
int x = Convert.ToInt64(stringVariable);
// wat if i give //99999999999999999999999999999999999999999999999999 999999999
//12345667890994645645656234234243245656465645645
//45345435345345345345
// this will cause overflow.... and wat abt double,
Catch
{
// then it isn't a number
}
i was lookin at something that can recognize any number as number... like of any length... n also double... also if possible currency or other standardized data ... its more like a puzzle program...
Sep 11 '08 #5
cloud255
427 Expert 256MB
i was lookin at something that can recognize any number as number... like of any length... n also double... also if possible currency or other standardized data ... its more like a puzzle program...
Nothing can hold infinite length data, so you need to put a maximum length on your input control or trim the user input. I suppose you could do a 'nested try catch' where you try to parse to each numeric type until it succeeds or in the final catch you know it is not a number.
Sep 11 '08 #6
Plater
7,872 Expert 4TB
Double.TryParse() is probably your best bet.
There should be overloads(at least on .Parse() there are) to dictate that a "$" might be in the number.
Sep 11 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Christopher Benson-Manica | last post by:
I'm trying to check whether a string is all digits. This part is easy: function allDigits( str ) { var foo=str.split( '' ); // better than charAt()? for( var idx=0; idx < foo.length; idx++ ) {...
14
by: Kayle | last post by:
How should we check if the '\0' characters exists in the string as I am confused that some books mentioned that we have to check whether we need to make sure that we pass the...
3
by: David Marsh | last post by:
The program calculates the continued fraction representation of the input: #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv) { double diff, n, r, i; if(argc != 2)...
5
by: Yuelin | last post by:
Hi there What's the best way of checking whether a textbox is empty? Thanks. Yuelin
6
by: Jan | last post by:
Hi, Is there any elegant way in VB.NET to check whether a certain string is part of a "collection" of strings. Something like IF "teststring" in {"string1", "string2", "string3",...
13
by: QQ | last post by:
for instance, I read a char from the input and I need to decide whether it is a letter or a number What I am doing is char a; ...... // read a int true = false; if( (a >='0') && (a <='z') true...
10
by: QQ | last post by:
for instance, I read a char from the input and I need to decide whether it is a letter or a number What I am doing is char a; ...... // read a int true = false; if(( (a >='0') && (a <='9')) | |...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
26
by: momobear | last post by:
hi, I am puzzled about how to determine whether an object is initilized in one class, anyone could give me any instructions? here is an example code: class coffee: def boil(self): self.temp =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.