Skip to main content

IsNumeric C# equivalent

IsNumeric in C#, WHY NOT?

This is something that has bothered me for a long while now. Why doesn't C# have a IsNumeric(string num) function like VB.NET? I have used every kind of IsNumeric code you van think of... This is what I started with.

public bool IsNumeric(string s) { try { Int32.Parse(s); } catch { return false; } return true; }

But we all no the unspoken rule, NEVER use try, catch for functionality... So I tried this.

internal static bool IsNumeric(string numberString) { char [] ca = numberString.ToCharArray(); for (int i = 0; i <> if (ca[i] > 57 || ca[i] <> return false; } return true; }

Then I found char.IsNumber: (Why do they have a char.IsNumber, but no string.IsNumeric???)

internal static bool IsNumeric(string numberString) { char [] ca = numberString.ToCharArray(); for (int i = 0; i <> if (!char.IsNumber(ca[i])) return false; } return true; }

public static bool IsNumeric(object Expression) { bool isNum; double retNum; isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any,System.Globalization.NumberFormatInfo.InvariantInfo, out retNum ); return isNum; }

Comments

Popular posts from this blog