Connecting Tech Pros Worldwide Forums | Help | Site Map

C#: whether string is number or not

PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 700
#1: Sep 10 '08
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.  

Member
 
Join Date: Apr 2008
Posts: 67
#2: Sep 10 '08

re: C#: whether string is number or not


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.
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 700
#3: Sep 10 '08

re: C#: whether string is number or not


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 ...
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,767
#4: Sep 11 '08

re: C#: whether string is number or not


try
{
int x = Convert.ToInt64(stringVariable);
Catch
{
// then it isn't a number
}
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 700
#5: Sep 11 '08

re: C#: whether string is number or not


Quote:

Originally Posted by tlhintoq

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...
Expert
 
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 410
#6: Sep 11 '08

re: C#: whether string is number or not


Quote:

Originally Posted by dirtbag

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.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#7: Sep 11 '08

re: C#: whether string is number or not


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.
Reply


Similar .NET Framework bytes