【发布时间】:2013-11-07 07:19:58
【问题描述】:
所以我想使用 TryParse 方法,但到目前为止我只能使用整数或双精度值。但是,我想检查该值是否是一个数字,如果不是(例如,如果它是一个字符串)得到一个假值。像 IsDigit() 这样的东西是 Java。
static void Main()
{
int number;
Console.Write("Enter a number: ");
bool result = Int32.TryParse(Console.ReadLine(), out number);
if (result)
{
Console.WriteLine("The input number is an integer.");
}
else
{
Console.WriteLine("The input number is not an integer.");
}
}
所以我想这样做,但不是检查整数值,而是检查数值。所以如果有人能告诉我我可以使用什么方法,我会很高兴。 提前致谢!
【问题讨论】:
-
“数值”是什么意思,为什么对整数或双精度数的 TryParse 不满意?
-
我还想知道 Boyan Kushlev 认为“数值”和双精度之间的区别是什么。什么字符串可以表示一个数字,但不能解析为双精度数?
-
任何非数字都会返回false,这里有什么问题??
-
@Tobberoth 1e123456789 是一个数字,但不能解析为双精度数。
-
@DavidArno 我同意,如果您不需要将数字作为数字变量,那么正则表达式似乎是更好的选择。