IsNumeric (VB) equivalent in C#

K

Kimelia Schiles

Hi,

I am new to C#, and I am a VB programmer. I would like to know whether there
is any function in C# that can is similar to IsNumeric in VB?
 
G

Guest

You can use Char.IsNumber(char) or RegEx to write your own function to check
the string IsNumeric or not. Or you can reference to the
Microsoft.VisualBasic to use the function IsNumeric.

function bool IsNumeric(string input)
{
bool result = true;
for (int i = 0; i < input.Length; i++)
{
if (!Char.IsNumber(input))
{
result = false;
break;
}
}
return result;
}
 
G

Guest

Or you can use try, catch to know string is numeric or not:

public bool IsNumeric(string s)
{
try {
Int32.Parse(s);
}
catch {
return false;
}
return true;
}

or you can try RegEx:

private static Regex RegExIsNumber = new Regex(@"^\d+$");

public static bool IsInteger(string input)
{
Match m = RegExIsNumber.Match(input);
return m.Success;
}


Hope this will help you
 
M

Morten Wennevik

There is also Double.TryParse which will never throw an exception, but merely return false if it fails to parse.
 
K

Kevin Spencer

Your function will fail on several different string formats of numbers, such
as:

0xA2B34
3.14159
123E+11
5,000

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

Minh Nguyen said:
You can use Char.IsNumber(char) or RegEx to write your own function to
check
the string IsNumeric or not. Or you can reference to the
Microsoft.VisualBasic to use the function IsNumeric.

function bool IsNumeric(string input)
{
bool result = true;
for (int i = 0; i < input.Length; i++)
{
if (!Char.IsNumber(input))
{
result = false;
break;
}
}
return result;
}

Kimelia Schiles said:
Hi,

I am new to C#, and I am a VB programmer. I would like to know whether
there
is any function in C# that can is similar to IsNumeric in VB?
 
K

Kevin Spencer

I tend to use Double.TryParse, except when a number falls outside of the
range of double. It works almost across the board.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
G

Guest

That should have been "There is *no* single method call..."
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter
 
A

Alan Popow

Hi,

I am new to C#, and I am a VB programmer. I would like to know whether there
is any function in C# that can is similar to IsNumeric in VB?

I assume you're not using VS.Net 2005. Anyway, you can actually use a lot of
your intrinsic VB stuff in C#.

Create a console project and set a reference to Microsoft.VisualBasic


using System;
using System.Text;
using Microsoft.VisualBasic;

namespace usingvbtest
{
class IsNumericTest
{
static void Main()
{
String str = "123456";
Console.WriteLine("{0}: {1}", str, Information.IsNumeric(str));
}
}
}

Alan
 
K

Kevin Spencer

Create a console project and set a reference to Microsoft.VisualBasic

In the case of IsNumeric, this is actually a pretty good idea in many cases.
The VB IsNumeric intrinsic function is pretty nicely designed. It is
culture-sensitive, and returns true if the data type is Boolean, Byte,
Decimal, Double, Integer, Long, SByte, Short, Single, UInteger, ULong, or
UShort. It also returns true if the parameter passed is a Char, String, or
Object that can be successfully converted to a number.

In short, if little is known about the data being evaluated, this can be
just the ticket. OTOH, it will not be as fast as, for example,
Double.TryParse, which can be used if the expected value falls withing
certain parameters and/or something is known about the data being evaluated.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 

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

Top