dataset 修改小数点位数

        #region dataset过滤器(修改小数点位数)导出使用
        public DataSet ChangeDataSetValue(DataSet dataset)
        {
            foreach (DataTable dt in dataset.Tables)   //遍历所有的datatable
            {
                //dt.Columns[i].DataType.ToString();

                foreach (DataRow dr in dt.Rows)
                {  ///遍历所有的行
                    //foreach (DataColumn dc in dt.Columns)
                    //{  //遍历所有的列
                    //}

                    foreach (DataColumn dc in dt.Columns)
                    {
                        Type type = dc.DataType;
                        string neam = type.Name;
                        if (neam == "decimal" || neam == "Decimal")
                        {
                            var value = dr[dc];
                            if (value == DBNull.Value)
                            {
                                value = 0;
                            }
                            decimal temp = Convert.ToDecimal(value);
                            dr[dc] = Math.Round(temp, 2);
                        }
                    }

                }
            }
            return dataset;
        }

        #endregion

  






#region dataset过滤器(修改小数点位数)导出使用 public DataSet ChangeDataSetValue(DataSet dataset) { foreach (DataTable dt in dataset.Tables) //遍历所有的datatable { foreach (DataRow dr in dt.Rows) { ///遍历所有的行 //foreach (DataColumn dc in dt.Columns) //{ //遍历所有的列 //} if (dt.Columns.Contains("PurchaseAmount")) { decimal PurchaseAmount = Convert.IsDBNull(dr["PurchaseAmount"]) ? 0 : Convert.ToDecimal(dr["PurchaseAmount"]); dr["PurchaseAmount"] = Math.Round(PurchaseAmount, 2); } if (dt.Columns.Contains("SalesAmount")) { decimal PurchaseAmount = Convert.IsDBNull(dr["SalesAmount"]) ? 0 : Convert.ToDecimal(dr["SalesAmount"]); dr["SalesAmount"] = Math.Round(PurchaseAmount, 2); } if (dt.Columns.Contains("DxdaAmount")) { decimal DxdaAmount = Convert.IsDBNull(dr["DxdaAmount"]) ? 0 : Convert.ToDecimal(dr["DxdaAmount"]); dr["DxdaAmount"] = Math.Round(DxdaAmount, 2); } if (dt.Columns.Contains("UnpayAmount")) { decimal UnpayAmount = Convert.IsDBNull(dr["UnpayAmount"]) ? 0 : Convert.ToDecimal(dr["UnpayAmount"]); dr["UnpayAmount"] = Math.Round(UnpayAmount, 2); } if (dt.Columns.Contains("NowAmount")) { decimal PurchaseAmount = Convert.IsDBNull(dr["NowAmount"]) ? 0 : Convert.ToDecimal(dr["NowAmount"]); dr["NowAmount"] = Math.Round(PurchaseAmount, 2); } if (dt.Columns.Contains("CoreAmount")) { decimal CoreAmount = Convert.IsDBNull(dr["CoreAmount"]) ? 0 : Convert.ToDecimal(dr["CoreAmount"]); dr["CoreAmount"] = Math.Round(CoreAmount, 2); } if (dt.Columns.Contains("DealerAmount")) { decimal DealerAmount = Convert.IsDBNull(dr["DealerAmount"]) ? 0 : Convert.ToDecimal(dr["DealerAmount"]); dr["DealerAmount"] = Math.Round(DealerAmount, 2); } } } return dataset; } #endregion




  

转载于:https://www.cnblogs.com/justqi/p/6170584.html

你可能感兴趣的:(dataset 修改小数点位数)