【问题标题】:Rounding down value after division, datatable datacolumn除法后四舍五入,数据表数据列
【发布时间】:2012-05-15 07:49:30
【问题描述】:

我想在应用除法后对列值进行四舍五入:

 DataColumn maxSpend = new DataColumn();
    maxSpend.DataType = System.Type.GetType("System.Double");
    maxSpend.Expression = string.Format("{0:0.00} / price", this.maxSpend);

有可能以某种方式在列表达式中使用 Math.floor() 还是有其他解决方案?

【问题讨论】:

  • 您是否要在此处将 this.maxSpend 除以价格 string.Format("{0:0.00} / price", this.maxSpend);

标签: c# datacolumn


【解决方案1】:

.NET 函数不允许在 DataColumn-Expressions 中使用。最简单的方法是首先在 dbms 中执行此操作(如果您正在使用)。

例如在SQL-ServerSELECT FLOOR(MaxSpend/Price)AS MaxSpend ...

另一种方法是循环遍历 DataRows。例如:

foreach (DataRow row in tbl.Rows)
{
    double price    = row.Field<double>("price");
    row["maxSpend"] = Math.Floor(this.maxSpend / price);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多