Products
Services

Scripts: C# :: Math Functions :: Library Article #4

Developer's Section

Determining whether a string is a number
By: Erob Team Member

 Hire a Developer for Related Work / Installation | \$55 hr
 Rating:  | Rate It: Average Votes: (3164) Favorites:

Learn to create a function to determine if the string given is a number.

The folloig function creates several regular expressions that help you determine whether the string passed as parameter is a number. This includes negative and positive numbers as well as decimals.

Example:

 Code Snippet 1

private bool IsNumber(String strNumber)
{
Regex objNotNumberPattern=new Regex("[^0-9.-]");
Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+\$";
String strValidIntegerPattern="^([-]|[0-9])[0-9]*\$";
Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
return !objNotNumberPattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber) &&
!objTwoMinusPattern.IsMatch(strNumber) &&
objNumberPattern.IsMatch(strNumber);

See other Scripts in Math Functions

 Submit Your Scripts: If you would like to have your Javascripts published in this section please fill outthe form below: