473,378 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

IsNumeric (VB) equivalent in C#

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
9 8957
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
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
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
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
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
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
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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joel | last post by:
In VBA, you can use IsNumeric to return True or False if the value being tested is numeric. Is there an equivalent to this in VB.NET?
7
by: Stephan Bour | last post by:
IS there a C# equivalent to the VB IsNumeric function?
7
by: Joe | last post by:
Hello All: Does anyone know how someone would check for a numeric value in .NET? In VB6, we used IsNumeric. What's the .NET equivalent? BTW, I know that I can harken back to IsNumeric. I'm...
7
by: Vikas Kumar | last post by:
For Each item In Request.Form If IsNumeric(item) Then Try Approved(item) Catch ex As Exception Response.Write(ex.Message) End Try End If Next
3
by: Vikas Kumar | last post by:
For Each item In Request.Form If IsNumeric(item) Then Try Approved(item) Catch ex As Exception Response.Write(ex.Message) End Try End If Next
8
by: moondaddy | last post by:
What's the .net framework equivalent of the vb function isnumeric? If there isn't one, how can I test a string variable to see if its a number or not? I don't want to use isnumeric if possible ...
12
by: Paul | last post by:
Hi, I am trying to check a string to see if it's first 3 characters are numeric and if they are, to replace those 3 characters with something else. I've tried this but nothing happens... ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.