IsNumeric in c#

by Dimi 23. October 2009 18:33

I’m currently working on a C# Windows forms application and was in need of a method which does the same as the good old Visual Basic method IsNumeric. Hahaha, sometimes the simplest things are hard to find. So I stepped over this Microsoft article: http://support.microsoft.com/kb/329488/de

What it does? It simply calls the method TryParse. The return value indicates whether it is numeric or not.

   1: static bool IsNumeric(object Expression)
   2: {
   3: // Variable to collect the Return value of the TryParse method.
   4:     bool isNum;
   5:  
   6: // Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
   7:     double retNum;
   8:             
   9: // The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
  10: // 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.
  11:     isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum );
  12:     return isNum;
  13: }        

Seems that this post would fit better in facebook ;)

Tags: , ,

development

Comments

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.0.0
Theme adopted by Dimi with portions of Mads Kristensen and portions of Rtur.net

RecentPosts