IsNumeric C# equivalent

S

Stephan Bour

Actually, IsNumeric does two things: converts a string and returns a boolean
true if the resulting expression is recognized as a number. Parse only
converts a string representation of a number to double. I need the boolean
return to validate a text box entry that can't be easily checked with a
validator.
Stephan.
 
S

Stephan Bour

That's the idea. However, I need to do that on a string. I guess I could use
the String.ToCharArray() method first or use a loop.
Thanks,
Stephan.
 
A

AlexS

Check Parse and TryParse methods for corresponding types, Int, Long etc.

HTH
Alex
 
S

Stephan Bour

I apologize, I read TryParse too fast. You're right, it does what I need.
Thank you,
Stephan.
 
A

AlexS

So, check TryParse

MSDN:
The TryParse method is like the Parse method, except this method does not
throw an exception if the conversion fails. If the conversion succeeds, the
return value is true and the result parameter is set to the outcome of the
conversion. If the conversion fails, the return value is false and the
result parameter is set to zero.



HTH

Alex
 
Joined
Jun 20, 2007
Messages
2
Reaction score
0
C# equivalent to the VB IsNumeric function

Hi guys i got a way around to get the functionality of IsNumeric(of VB.NET)
in C#. Please try it out.



Code:
using System;
using System.Text;
using System.Text.RegularExpressions;

private bool IsTextValidated(string strTextEntry)
        {           
            Regex objNotWholePattern = new Regex("[^0-9]");
            return !objNotWholePattern.IsMatch(strTextEntry);            
        }
 
Joined
Jul 24, 2009
Messages
1
Reaction score
0
returns true for null...

The function returns true if the value is null.

So for a workaround;

Code:
using System;
using System.Text;
using System.Text.RegularExpressions;

private bool IsTextValidated(string strTextEntry)
{           
      Regex objNotWholePattern = new Regex("[^0-9]");
      return !objNotWholePattern.IsMatch(strTextEntry)
           [COLOR="Blue"]&& (strTextEntry != ""); [/COLOR]     
}
 
Joined
Mar 22, 2011
Messages
1
Reaction score
0
The old IsNumeric function is still available in .NET

Add a reference to Microst.Visualbasic and use the Information.IsNumeric function.

e.g.

using Microsoft.VisualBasic;
Code:
    private void button1_Click(object sender, EventArgs e) {
      string s = "1";
      if (Information.IsNumeric(s)) {
        MessageBox.Show("yes it's a number");
      }   
    }


P.S. I know this is a 7 year old thread, but, this page is the first one that appears if you Google for C# IsNumeric and it does not have a correct answer. Just trying to fight pollution of the internet.
 
Last edited:
Joined
Jan 13, 2012
Messages
2
Reaction score
0
C# equivalent of IsNumeric

This seems to work well:

Code:
public static bool IsNumeric(object o)
{
	double result;
	return o != null && Double.TryParse(o.ToString(), out result);
}
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top