Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the C# equivalent of NaN or IsNumeric?

Tags:

c#

parsing

What is the most efficient way of testing an input string whether it contains a numeric value (or conversely Not A Number)? I guess I can use Double.Parse or a regex (see below) but I was wondering if there is some built in way to do this, such as javascript's NaN() or IsNumeric() (was that VB, I can't remember?).

public static bool IsNumeric(this string value) {     return Regex.IsMatch(value, "^\\d+$"); } 
like image 979
johnc Avatar asked Jan 13 '09 03:01

johnc


People also ask

What is C used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language in simple words?

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is -= in C?

-= Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. C -= A is equivalent to C = C - A.

What is computer C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


2 Answers

I prefer something like this, it lets you decide what NumberStyle to test for.

public static Boolean IsNumeric(String input, NumberStyles numberStyle) {     Double temp;     Boolean result = Double.TryParse(input, numberStyle, CultureInfo.CurrentCulture, out temp);     return result; } 
like image 36
Anthony Mastrean Avatar answered Oct 11 '22 18:10

Anthony Mastrean


This doesn't have the regex overhead

double myNum = 0; String testVar = "Not A Number";  if (Double.TryParse(testVar, out myNum)) {   // it is a number } else {   // it is not a number } 

Incidentally, all of the standard data types, with the glaring exception of GUIDs, support TryParse.

update
secretwep brought up that the value "2345," will pass the above test as a number. However, if you need to ensure that all of the characters within the string are digits, then another approach should be taken.

example 1:

public Boolean IsNumber(String s) {   Boolean value = true;   foreach(Char c in s.ToCharArray()) {     value = value && Char.IsDigit(c);   }    return value; } 

or if you want to be a little more fancy

public Boolean IsNumber(String value) {   return value.All(Char.IsDigit); } 

update 2 ( from @stackonfire to deal with null or empty strings)

public Boolean IsNumber(String s) {      Boolean value = true;      if (s == String.Empty || s == null) {          value=false;      } else {          foreach(Char c in s.ToCharArray()) {              value = value && Char.IsDigit(c);          }      } return value;  } 
like image 93
NotMe Avatar answered Oct 11 '22 18:10

NotMe