Numeric or not
How to check a field ..whether is it numberic or not. in Visaul Stdio2005 C#
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
raihan682 pointsPosted Feb 15, 2006, 11:42 PM EST
Solitaire85 pointsPosted Feb 16, 2006, 8:41 AM EST
The following emulates the Val() function of VB. It returns the numeric value of a string up to the first non-numeric character. Otherwise it returns 0. It works with positive or negative numbers and will accept a single decimal point. However, you will probably prefer a bool method to emulate IsNumeric().
public static double Valcs(string snum) { double vnum = 0; int N; snum = snum.Replace(" ", ""); N = snum.Length; string[] anum = new string[N + 1]; for (int x = 1; x <= N; x++) { anum[x] = snum.Substring(0, x); try { vnum = Convert.ToDouble(anum[x]); } catch { if(x != 0 && anum[x] != "-") break; } } return vnum; }