Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //determine whether the input value is a number
- public static bool IsNumeric(string someValue)
- {
- Regex isNumber = new Regex(@"^d+$");
- try
- {
- Match m = isNumber.Match(someValue);
- return m.Success;
- }
- catch (FormatException)
- {return false;}
- }
- int x;
- double y;
- string spork = "-3.14";
- if (int.TryParse(spork, out x))
- Console.WriteLine("Yay it's an int (boy)!");
- if (double.TryParse(spork, out y))
- Console.WriteLine("Yay it's an double (girl)!");
- Regex isNumber = new Regex(@"^[-+]?(d*.)?d+$");
- // Determine whether the input value is a number
- public static bool IsNumeric(string someValue)
- {
- return new Regex(@"^[-+]?(d*.)?d+$").IsMatch(someValue);
- }
- ^-?d+$
- ^-?d*.?d*$
- ^-?d*.?d*(ed+)?$
- public static bool IsNumeric(this string value)
- {
- double temp;
- return double.TryParse(value.ToString(), out temp);
- }
- string someValue = "89.9";
- if (someValue.IsNumeric()) // will be true in the US, but not in Sweden
- {
- // wow, it's a number!
- ]
- public static bool IsNumeric(string value)
- {
- bool isNumber = true;
- bool afterDecimal = false;
- for (int i=0; i<value.Length; i++)
- {
- char c = value[i];
- if (c == '-' && i == 0) continue;
- if (Char.IsDigit(c))
- {
- continue;
- }
- if (c == '.' && !afterDecimal)
- {
- afterDecimal = true;
- continue;
- }
- isNumber = false;
- break;
- }
- return isNumber;
- }
Add Comment
Please, Sign In to add comment