Is there an Int.isWholeNumber () function or something similar?

I need to check if the input is int or not. Is there a similar function for String.IsNullOrEmpty (), like the Int.isWholeNumber () function?

Is there a way to check this inside a statement if()without having the right to declare an int earlier? (How do you need to do with TryParse ())

EDIT
I need to check the area code (five numbers)

+3
source share
8 answers

I do not believe that there is such a method in BCL, but it is easy to write (I assume that you are really talking about whether a string can be parsed as an integer):

public static bool CanParse(string text)
{
    int ignored;
    return int.TryParse(text, out ignored);
}

, IFormatProvider .., . , , , ...

, Fluent Validation.

+10

, - " , ?" - , . , . , , , , :

  • - ? , .
  • - 1, 2, 3, 4, 5, 6, 7, 8 9? , .
  • - , , 0, 1, 2, 3, 4, 5, 6, 7, 8 9? , .

. , .

. , , "SE-12 345", "SE-" . , , .

. , . - , . , - , . , -, , ; , .

+12

:

bool isNumber = "-1990".IsNumber();

:

public static class NumberStringExtension
{

    public static bool IsNumber(this string value)
    {
        int i = 0;
        return int.TryParse(value, out i));
    }

}
+2

Int.TryParse .

function bool IsNumber(object number)
{
  try
  {
    Convert.ToInt32(number.ToString());
    return true;
  }
  catch
  {
   return false;
  }
}
+1

, int.TryParse , , . ( , - "1." .)

string input;
int ignored;

bool wholeNumber = int.TryParse(input, out ignored) && input.indexOf('.') == -1;
+1

Int types must be integers by definition. It must be a double, float, or decimal type for a non-integer. You can always try to check the input type.

Or maybe the input is a string? If the input is a string, you can try a search. or ',' (depending on the culture). Or you can try parsing as an integer.

0
source

Since each int is an integer, this is easy:

public static bool IsWholeNumber(int i)
{
    return true;
}
0
source

If you just want to check it in the if statement, you can just do

If ( val % 1 > 0)
  //not whole number
-2
source

All Articles