0

我只想对来自数据表的值进行四舍五入,数据来自 SQL 到数据表。数据表也有空值我猜这是问题,但也请你帮我检查

string Maxmonthlytable = Math.Round((decimal)monthlytable.Rows[u][3], 2, MidpointRounding.AwayFromZero).ToString();

我收到错误“从数字转换时,值必须是小于无穷大的数字”

4

3 回答 3

2

您可以使用DataRow.Field支持nullables的方法:

decimal? num = monthlytable.Rows[u].Field<decimal?>(3);
Console.Write(num.HasValue 
                 ? Math.Round(num.Value, MidpointRounding.AwayFromZero)
                 : "no value");
于 2012-10-23T07:43:58.457 回答
0

你可以试试:

decimal num = 0;
if (monthlytable.Rows[u][3] != DBNull.Value)
    num = Math.Round((decimal)monthlytable.Rows[u][3], 2, MidpointRounding.AwayFromZero)
string Maxmonthlytable = num.ToString();
于 2012-10-23T07:29:20.100 回答
-1

Instead of using the cast (decimal) or using the cast (int), you have to use the cast (System.Decimal) or (System.Int32) and that will make this problem go away.

于 2013-04-08T23:04:36.270 回答