是否有一个Int.isWholeNumber()函数或类似的东西? [英] Is there an Int.isWholeNumber() function or something similar?

查看:345
本文介绍了是否有一个Int.isWholeNumber()函数或类似的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查,如果输入是一个int或没有。是否有类似功能的String.IsNullOrEmpty(),像一个Int.isWholeNumber()函数?

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?

有没有一种方法来验证这个<$ C $内C>如果()语句只,而无需之前声明一个int? (当你需要的TryParse()做)

Is there a way to validate this inside the if() statement only, without having to declare an int before? (As you need to do with TryParse())

修改结果
我需要验证区号(5数字)

EDIT
I need to validate an area code (five numbers)

推荐答案

我不相信还有的BCL内的任何这样的方法,但它很容易写一个(我假设你真的在谈论一个字符串是否可以被解析为整数):

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

添加重载接受的IFormatProvider 值等,你需要它们。假设这是验证,您可能希望将它扩大到允许有效值范围进行,以及指定的...

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.

这篇关于是否有一个Int.isWholeNumber()函数或类似的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆