Home > ASP.NET, C#, Software Development, VB.NET, VBScript, Web Development > How to implement IsNumeric() VB.NET function in C#

How to implement IsNumeric() VB.NET function in C#

This article describes how to implement the useful “IsNumeric” function in Microsoft Visual Basic .NET in Visual C#. The IsNumeric() function returns a Boolean value indicating whether an expression can be evaluated as a number. The IsNumeric() function returns True when the data type of the expression is Short, Integer, Long, Decimal, Single or Double. Returns True if the expression is a string that can be converted to a Double.

Add the following code to your project:

// IsNumeric Function
static bool IsNumeric(object Expression)
{
// Variable to collect the Return value of the TryParse method.
	bool isNum;

// Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
	double retNum;
			
// The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
// The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
	isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum );
	return isNum;
}	

Have a nice day!

Max :)

Advertisement
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 32 other followers