指定的格式无效"DataTable.Compute
本文关键字:quot DataTable Compute 无效 格式 | 更新日期: 2023-09-27 18:17:07
这里有这样的代码:
decimal dec = (decimal)MyDataTable.Compute("Min(Rooms)", string.Empty);
它总是告诉我指定的强制转换无效。如何在运行时检查计算是否成功?
谢谢:)
试试这个
object dec = MyDataTable.Compute("Min(Rooms)", string.Empty);
decimal d;
bool result = Decimal.TryParse(dec.ToString(), out d);
如果result
是true
,则表示解析成功
尝试使用Decimal.TryParse()
看看是否有帮助。这是msdn这应该可以工作:
var s = "123.34";
decimal d;
bool isDec = Decimal.TryParse(s, out d);
if (isDec)
Console.WriteLine("It was a decimal: " + d);
else
Console.WriteLine("Not a decimal!");
Console.WriteLine(isDec);
Console.ReadLine();
需要两个参数的原因是第一个参数是以字符串格式表示的十进制。如果TryParse成功,结果将存储在d
中(在上面的示例中)。在上面的示例中,isDec
打印true
。