By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,089 Members | 1,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,089 IT Pros & Developers. It's quick & easy.

IsNumeric (VB) equivalent in C#

P: n/a
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?
Apr 11 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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[i]))
{
result = false;
break;
}
}
return result;
}

"Kimelia Schiles" wrote:
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?

Apr 11 '06 #2

P: n/a
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
"Kimelia Schiles" wrote:
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?

Apr 11 '06 #3

P: n/a
There is also Double.TryParse which will never throw an exception, but merely return false if it fails to parse.
On Tue, 11 Apr 2006 05:26:02 +0200, Minh Nguyen <Mi********@discussions.microsoft.com> wrote:
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
"Kimelia Schiles" wrote:
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?


--
Happy coding!
Morten Wennevik [C# MVP]
Apr 11 '06 #4

P: n/a
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" <Mi********@discussions.microsoft.com> wrote in message
news:A0**********************************@microsof t.com...
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[i]))
{
result = false;
break;
}
}
return result;
}

"Kimelia Schiles" wrote:
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?

Apr 11 '06 #5

P: n/a
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.

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op.s7ttm4a5klbvpo@stone...
There is also Double.TryParse which will never throw an exception, but
merely return false if it fails to parse.

Apr 11 '06 #6

P: n/a
There is single method call in the .NET Framework that will do this.
Check out this link for a typical method that will simulate IsNumeric
reasonably closely.
http://www.tangiblesoftwaresolutions...0IsNumeric.htm
--
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

"Kimelia Schiles" wrote:
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?

Apr 11 '06 #7

P: n/a
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

"David Anton" wrote:
There is single method call in the .NET Framework that will do this.
Check out this link for a typical method that will simulate IsNumeric
reasonably closely.
http://www.tangiblesoftwaresolutions...0IsNumeric.htm
--
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

"Kimelia Schiles" wrote:
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?

Apr 11 '06 #8

P: n/a
On Tue, 11 Apr 2006 10:34:19 +0800, "Kimelia Schiles" <ka**@hotmail.com>
wrote:
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
Apr 12 '06 #9

P: n/a
> 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.

"Alan Popow" <al*****@sympatico.ca> wrote in message
news:ma********************************@4ax.com...
On Tue, 11 Apr 2006 10:34:19 +0800, "Kimelia Schiles" <ka**@hotmail.com>
wrote:
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

Apr 12 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.