0 Members and 1 Guest are viewing this topic.
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;}
public static bool IsNumeric(string theValue){ Regex _isNumber = new Regex(@"^\d+$"); Match m = _isNumber.Match(theValue); return m.Success;}
string strNum;Regex oPattern = new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$");oPattern.IsMatch(strNum);
............. how about something like this, Kerry:Code: [Select]string strNum;Regex oPattern = new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$");oPattern.IsMatch(strNum);Without any test....
If you compile the VB method, what does reflector show?
public static bool IsNumeric(object Expression){ double result; IConvertible convertible = Expression as IConvertible; if (convertible == null) { char[] chArray = Expression as char[]; if (chArray == null) { return false; } Expression = new string(chArray); } TypeCode typCode = convertible.GetTypeCode(); if ((typCode != TypeCode.String) && (typCode != TypeCode.Char)) { return IsOldNumericTypeCode(typCode); } string text = convertible.ToString(null); try { long num2; if (Utils.IsHexOrOctValue(text, ref num2)) { return true; } } catch (StackOverflowException exception) { throw exception; } catch (OutOfMemoryException exception2) { throw exception2; } catch (ThreadAbortException exception3) { throw exception3; } catch (Exception) { return false; } return DoubleType.TryParse(text, ref result);}
public bool isNumeric(string val, NumberStyles NumberStyle){ Double result; return Double.TryParse(val, NumberStyle, CultureInfo.CurrentCulture, out result);}
string strNum1 = "0231.3213", strNum2 = "asad1213.34", strNum3 = ".2342314", strNum4 = "ADSAD", strNum5 = "-1";if (isNumeric(strNum1, NumberStyles.Number)) //(IsNumber(strNum1) == true) ed.WriteMessage("\n" + strNum1 + " is a number!\n");else ed.WriteMessage(strNum1 + " is not a number!\n");if (isNumeric(strNum2, NumberStyles.Number)) ed.WriteMessage(strNum2 + " is a number!\n");else ed.WriteMessage(strNum2 + " is not a number!\n");if (isNumeric(strNum3, NumberStyles.Number)) ed.WriteMessage(strNum3 + " is a number!\n");else ed.WriteMessage(strNum3 + " is not a number!\n");if (isNumeric(strNum4, NumberStyles.Number)) ed.WriteMessage(strNum4 + " is a number!\n");else ed.WriteMessage(strNum4 + " is not a number!\n");if (isNumeric(strNum5, NumberStyles.Number)) ed.WriteMessage(strNum5 + " is a number!\n");else ed.WriteMessage(strNum5 + " is not a number!\n");
0231.3213 is a number!asad1213.34 is a number!.2342314 is a number!ADSAD is not a number!-1 is a number![/qoute]
asad1213.34 is a number!
Quoteasad1213.34 is a number!?