我试图创建一个确定的数是真正的数字简单的辅助函数。很明显,它应该能够处理'空',负数,我试图做到这一点没有VB的IsNumeric的帮助。刚学过LINQ,我认为那会很完美。则IsNumeric helper方法对大量
我想其他的事情是能够传递一个字符串,整数,长,或任何其他类型的,所以我想有一个“对象”作为参数,是我真正想要的。当然,我总是可以的类型转换为字符串调用辅助方法之前,但它是可能?
这里是我到目前为止的代码和所有我需要做的是能够改变的参数!我无法想象这是不可能的......任何想法?
private static bool IsNumeric(string input)
{
if (input == null) throw new ArgumentNullException("input");
if (string.IsNullOrEmpty(input)) return false;
int periodCount = 0; //accept a string w/ 1dec to support values w/ a float
return input.Trim()
.ToCharArray()
.Where(c =>
{
if (c == '.') periodCount++;
return Char.IsDigit(c) && periodCount <= 1;
})
.Count() == input.Trim().Length;
}
为什么不直接用正则表达式测试(未经测试,从内存中): '@ “[ - +] [0-9] +?(\ [0-9] *)?”' – 2013-05-04 21:36:14
或者使用decimal.TryParse或float.TryParse或double.TryParse? – 2013-05-04 21:37:18
“大”号码是什么意思? “大”有多大? – 2013-05-04 23:14:31