What Is the C# Equivalent of Nan or Isnumeric

What is the C# equivalent of NaN or IsNumeric?

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;
}

Lack of IsNumeric function in C#

Try this:

int temp;
return int.TryParse(input, out temp);

Of course, the behavior will be different from Visual Basic IsNumeric. If you want that behavior, you can add a reference to "Microsoft.VisualBasic" assembly and call the Microsoft.VisualBasic.Information.IsNumeric function directly.

Does a Type represent a numeric value C#

If you look at Linq ExpressionNode (internal class) IsNumeric method, it is basically testing against every type.

if (!IsFloat(type))
{
return IsInteger(type);
}
return true;

And the two function are testing against the primitive type, like

internal static bool IsInteger(StorageType type)
{
if ((((type != StorageType.Int16) && (type != StorageType.Int32)) && ((type != StorageType.Int64) && (type != StorageType.UInt16))) && (((type != StorageType.UInt32) && (type != StorageType.UInt64)) && (type != StorageType.SByte)))
{
return (type == StorageType.Byte);
}
return true;
}

StorageType is a Linq specific class, but you get the idea: just test against every type.

It is the easiest way to know if the value is a numeric type.

Checking if an object is a number

You will simply need to do a type check for each of the basic numeric types.

Here's an extension method that should do the job:

public static bool IsNumber(this object value)
{
return value is sbyte
|| value is byte
|| value is short
|| value is ushort
|| value is int
|| value is uint
|| value is long
|| value is ulong
|| value is float
|| value is double
|| value is decimal;
}

This should cover all numeric types.

Update

It seems you do actually want to parse the number from a string during deserialisation. In this case, it would probably just be best to use double.TryParse.

string value = "123.3";
double num;
if (!double.TryParse(value, out num))
throw new InvalidOperationException("Value is not a number.");

Of course, this wouldn't handle very large integers/long decimals, but if that is the case you just need to add additional calls to long.TryParse / decimal.TryParse / whatever else.

C#: How to write another function for this code?

static void Main(string[] args)
{
int number = NoNegatives();
}
static int NoNegatives()
{
int numberOut;

string numberIn = Console.ReadLine();

while (!int.TryParse(numberIn, out numberOut) || numberOut < 0)
{
Console.WriteLine("Invalid. Enter a number that's 0 or higher.");
numberIn = Console.ReadLine();
}
return numberOut;
}

How to block invalid input with a textbox message and refuse inputs which are not numbers

There are many ways to do this, two that spring to mind are that you could loop over the characters in the box, or use regex

//Looping
bool textChar = false;
foreach(char letter in NumberTextBoxInput)
{
if(!Char.IsNumber(letter))
{
textChar = true;
break;
}
}
if(textChar)
MessageBox.Show("Please enter a valid number.");
else
{
//Rest of method
}

Alternatively

//Regex
Regex reg = new Regex(@"\D");
if(reg.IsMatch(NumberTextBoxInput))
{
MessageBox.Show("Please enter a valid number.");
}
else
{
//Rest of method
}


Related Topics



Leave a reply



Submit