C# ASP.net 后端数据处理汇整

1、日期格式带有 T/Z 字样的处理

例:

a.创建表接收Model数据

DataTable tbl = new DataTable();
tbl.Columns.Add("deviceName", typeof(String)); //设备名称
tbl.Columns.Add("productName", typeof(String)); //产品名称
tbl.Columns.Add("theQAD", typeof(String)); //QAD代码
tbl.Columns.Add("theStartTime", typeof(String)); //开始时间
tbl.Columns.Add("theEndTime", typeof(String)); //结束时间
tbl.Columns.Add("productnum", typeof(int)); //产量
tbl.Columns.Add("theEle", typeof(Double)); //耗电量
tbl.Columns.Add("theEveryEle", typeof(Double)); //每件耗能

b.循环赋值,对日期格式处理

List<Model.ModuleYF.DeviceProductInfo> models = BLL.ModuleYF.DeviceManager.GetAllDeviceProductInfo(enterpriseId); 
foreach (Model.ModuleYF.DeviceProductInfo model in models)
            {
                DataRow row = tbl.NewRow();
                row["deviceName"] = model.DeviceName;
                row["productName"] = model.ProductName;

                row["theQAD"] = model.TheQAD;
                row["theStartTime"] = model.TheStartTime.ToString("yyyy-MM-dd HH:mm:ss");
                row["theEndTime"] = model.TheEndTime.ToString("yyyy-MM-dd HH:mm:ss");
                row["productnum"] = model.Productnum;
                row["theEle"] = model.TheEle;
                row["theEveryEle"] = Math.Round(Convert.ToDecimal(model.TheEle / model.Productnum),4, MidpointRounding.AwayFromZero);
                tbl.Rows.Add(row);
            }

c.转换成JSON 传递到前台显示,则日期不在含有 T/Z 字样

 public class JSON
    {
        public static string DateTimeFormat;

        public JSON();

        public static object Decode(string json);
        public static object Decode(string json, Type type);
        public static string Encode(object o);
    } 

string json = JSON.Encode(tbl);
            Response.Write(json);

2、小数点取有效值位数

Math.Round(Convert.ToDecimal(2.222457),3, MidpointRounding.AwayFromZero);//结果 2.222

猜你喜欢

转载自blog.csdn.net/qq_21419015/article/details/81331683