Jump to content
Xtreme .Net Talk

Recommended Posts

  • Administrators
Posted
You could use regular expressions to validate the string (search these forums and you'll will find a few samples), or alternatively you may want to use the double.TryParse(...) method - incidently if you look at the VB IsNumeric function in ildasm then it actually calls the TryParse method itself anyway....

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Something like:

 

internal static double IsNumeric(object oValue)

{

try

{

if (oValue == null || System.Convert.IsDBNull(oValue))

return false;

 

string sString = oValue.ToString().Trim();

 

double dValue=0;

if (double.TryParse(sString, NumberStyles.Any,

CultureInfo.CurrentCulture, out dValue))

return true;

else

return false;

}

catch

{

return false;

}

}

  • 3 weeks later...
Posted

Just reference Microsoft.VisualBasic and you can use the VB IsNumeric function in C#.

 

Otherwise, just do something like this:

private bool IsNumeric(object o)
{
   try { double d = Convert.ToDouble(o); return true; }
   catch { return false; }
}

Posted
Just reference Microsoft.VisualBasic and you can use the VB IsNumeric function in C#.

[/Quote]BOO!!!! Bad Vb'er Bad!!!

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted
hi in VB.net i can use ISNumeric to determine if it's numeric or not but in C# i can't use it can any body help

 

Try this out Tamer:

 

   Public Overloads Function IsNumeric(ByVal Value As Object) As Boolean
       Dim RegEx As Regex

           If Not Value Is Nothing Then
               RegEx = New Regex("(\+|-)?[0-9][0-9]*(\.[0-9]*)?")
               If RegEx.IsMatch(Value.ToString) AndAlso RegEx.Match(Value.ToString).Length = CType(Value, String).Length Then
                   Return True
               Else
                   Return False
               End If
           Else
               Return False
           End If

 End Function

 

You should be able to easily convert this to c#.

  • Administrators
Posted

Although the regex works it does have a locale bias (assumes .) as decimal seperator, and will fail if a thousands seperator is specified (or if a trailing +/- is used).

Also it will not cope with currency or non decimal (e.g. hex) numbers.

 

Using double.TryParse like I suggested an Jaco showed how to do in a previous post in this thread is a easy enough way to do this.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...