Guest User

Untitled

a guest
May 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. //determine whether the input value is a number
  2. public static bool IsNumeric(string someValue)
  3. {
  4. Regex isNumber = new Regex(@"^d+$");
  5. try
  6. {
  7. Match m = isNumber.Match(someValue);
  8. return m.Success;
  9. }
  10. catch (FormatException)
  11. {return false;}
  12. }
  13.  
  14. int x;
  15. double y;
  16. string spork = "-3.14";
  17.  
  18. if (int.TryParse(spork, out x))
  19. Console.WriteLine("Yay it's an int (boy)!");
  20. if (double.TryParse(spork, out y))
  21. Console.WriteLine("Yay it's an double (girl)!");
  22.  
  23. Regex isNumber = new Regex(@"^[-+]?(d*.)?d+$");
  24.  
  25. // Determine whether the input value is a number
  26. public static bool IsNumeric(string someValue)
  27. {
  28. return new Regex(@"^[-+]?(d*.)?d+$").IsMatch(someValue);
  29. }
  30.  
  31. ^-?d+$
  32.  
  33. ^-?d*.?d*$
  34.  
  35. ^-?d*.?d*(ed+)?$
  36.  
  37. public static bool IsNumeric(this string value)
  38. {
  39. double temp;
  40. return double.TryParse(value.ToString(), out temp);
  41. }
  42.  
  43. string someValue = "89.9";
  44. if (someValue.IsNumeric()) // will be true in the US, but not in Sweden
  45. {
  46. // wow, it's a number!
  47. ]
  48.  
  49. public static bool IsNumeric(string value)
  50. {
  51. bool isNumber = true;
  52.  
  53. bool afterDecimal = false;
  54. for (int i=0; i<value.Length; i++)
  55. {
  56. char c = value[i];
  57. if (c == '-' && i == 0) continue;
  58.  
  59. if (Char.IsDigit(c))
  60. {
  61. continue;
  62. }
  63.  
  64. if (c == '.' && !afterDecimal)
  65. {
  66. afterDecimal = true;
  67. continue;
  68. }
  69.  
  70. isNumber = false;
  71. break;
  72. }
  73.  
  74. return isNumber;
  75. }
Add Comment
Please, Sign In to add comment