首页 > 解决方案 > 如何在 c# 中使用 datatable.compute 进行除法?

问题描述

我在 C# 中使用 divion 运算符,如下所示,并得到一个错误,比如Specified cast is not valid它背后的任何原因

    DataTable dt = new DataTable();
    int answer = (int)dt.Compute("(1*4000*700*20)/4000", ""); 

标签: c#.net

解决方案


使用Convert()而不是强制转换来获得int结果

int answer = Convert.ToInt32(new DataTable().Compute("(1*4000*700*20)/4000", ""));

推荐阅读