Is there an Int.isWholeNumber() function or something similar?
I need to check if an input is an int or not. Is there a similar function to the String.IsNullOrEmpty(), like an Int.isWholeNumber() function?
Is there开发者_如何学Go a way to validate this inside the if()
statement only, without having to declare an int before? (As you need to do with TryParse())
EDIT
I need to validate an area code (five numbers)I don't believe there's any such method within the BCL, but it's easy to write one (I'm assuming you're 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);
}
Add overloads accepting IFormatProvider
values etc as you require them. Assuming this is for validation, you might want to expand it to allow a range of valid values to be specified as well...
If you're doing a lot of custom validation, you may well want to look at the Fluent Validation project.
I gather from your comments that your actual problem is "how do I determine if this string contains a valid Swedish postal code?" A Swedish postal code is a five digit number not beginning with a zero. If that's the problem you actually have to solve, then solve that problem. Rather than trying to convert the string to an integer and then check the integer, I would simply write checks that say:
- is the string five characters long? If not, reject it.
- is the first character of the string 1, 2, 3, 4, 5, 6, 7, 8 or 9? If not, reject it.
- are the second, third, fourth and fifth characters of the string 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9? If not, reject it.
Simple as that. If you're never going to do math on it, don't convert it to an integer in the first place.
This approach will further generalize to more complex forms. Swedish postal codes, I gather, are often written in the form "SE-12 345", that is, with the prefix "SE-" and a space between digits two and three. It's going to be awfully hard to write an integer-validating routine that deals with that format, but writing a string-validating routine is straightforward.
More generally, this illustrates some good advice for writing questions. Ask a question about the problem you actually must solve. You assumed a solution -- parse the string as an integer -- and then started asking questions about your assumed solution. That automatically precludes anyone from giving advice that is specific to your real problem. Maybe someone reading this has already developed a library of postal-code validating software; if they have, they'd never know to tell you about it from your original question.
You can achieve this by extension methods:
You use
bool isNumber = "-1990".IsNumber();
Here is code:
public static class NumberStringExtension
{
public static bool IsNumber(this string value)
{
int i = 0;
return int.TryParse(value, out i));
}
}
See if int.TryParse gives you an answer, and make sure there's not a decimal point in the input string. (This will give you a false positive if someone enters "1." as the input.)
string input;
int ignored;
bool wholeNumber = int.TryParse(input, out ignored) && input.indexOf('.') == -1;
Use Int.TryParse
Or make a custom function.
function bool IsNumber(object number)
{
try
{
Convert.ToInt32(number.ToString());
return true;
}
catch
{
return false;
}
}
int types have to be whole numbers by definition. It would have to be type double, float, or decimal to be a non-whole number. You can always try testing the type of the input.
Or maybe the input is a string? If the input is a string, you could try searching for a '.' or ',' character (depending on the culture). Or you could try parsing as an integer.
Since every int is a whole number, this is easy:
public static bool IsWholeNumber(int i)
{
return true;
}
If you just want to validate it in the if statement you could just do
If ( val % 1 > 0)
//not whole number
精彩评论