• 数据转换扩展类


        /// <summary>
        /// 数据转换扩展类型
        /// </summary>
        public static class DataConvertExtend
        {
            /// <summary>
            /// 对象转成字符
            /// </summary>
            /// <param name="obj">对象</param>
            /// <returns>字符</returns>
            public static string DataToString(this object obj)
            {
                if (obj == null || obj == DBNull.Value)
                {
                    return string.Empty;
                }
    
                return obj.ToString();
            }
    
            /// <summary>
            /// 对象转成整型
            /// </summary>
            /// <param name="obj">对象</param>
            /// <returns>整型</returns>
            public static int DataToInt(this object obj)
            {
                if (obj == null || obj == DBNull.Value)
                {
                    return 0;
                }
    
                int data = int.MinValue;
                if (int.TryParse(obj.ToString(), out data) == false)
                {
                    return 0;
                }
    
                return data;
            }
    
            /// <summary>
            /// 对象转换成整型
            /// </summary>
            /// <param name="obj">对象</param>
            /// <returns>整型</returns>
            public static int? DataToIntMark(this object obj)
            {
                if (obj == null || obj == DBNull.Value)
                {
                    return null;
                }
    
                int data = int.MinValue;
                if (int.TryParse(obj.ToString(), out data) == false)
                {
                    return null;
                }
    
                return data;
            }
    
            /// <summary>
            /// 对象转成Float
            /// </summary>
            /// <param name="obj">对象</param>
            /// <returns>Float</returns>
            public static float DataToFloat(this object obj)
            {
                if (obj == null || obj == DBNull.Value)
                {
                    return 0;
                }
    
                float data = float.MinValue;
                if (float.TryParse(obj.ToString(), out data) == false)
                {
                    return 0;
                }
    
                return data;
            }
    
            /// <summary>
            /// 对象转换成Float
            /// </summary>
            /// <param name="obj">对象</param>
            /// <returns>Float</returns>
            public static float? DataToFloatMark(this object obj)
            {
                if (obj == null || obj == DBNull.Value)
                {
                    return null;
                }
    
                float data = float.MinValue;
                if (float.TryParse(obj.ToString(), out data) == false)
                {
                    return null;
                }
    
                return data;
            }
    
            /// <summary>
            /// 对象转成double
            /// </summary>
            /// <param name="obj">对象</param>
            /// <returns>double</returns>
            public static double DataToDouble(this object obj)
            {
                if (obj == null || obj == DBNull.Value)
                {
                    return 0;
                }
    
                double data = double.MinValue;
                if (double.TryParse(obj.ToString(), out data) == false)
                {
                    return 0;
                }
    
                return data;
            }
    
            /// <summary>
            /// 对象转换成double
            /// </summary>
            /// <param name="obj">对象</param>
            /// <returns>double</returns>
            public static double? DataToDoubleMark(this object obj)
            {
                if (obj == null || obj == DBNull.Value)
                {
                    return null;
                }
    
                double data = double.MinValue;
                if (double.TryParse(obj.ToString(), out data) == false)
                {
                    return null;
                }
    
                return data;
            }
    
            /// <summary>
            /// 对象转成浮点类型
            /// </summary>
            /// <param name="obj">对象</param>
            /// <returns>浮点类型</returns>
            public static decimal DataToDecimal(this object obj)
            {
                if (obj == null || obj == DBNull.Value)
                {
                    return 0;
                }
    
                decimal data = decimal.MinValue;
                if (decimal.TryParse(obj.ToString(), out data) == false)
                {
                    return 0;
                }
    
                return data;
            }
    
            /// <summary>
            /// 对象转换成浮点类型
            /// </summary>
            /// <param name="obj">对象</param>
            /// <returns>浮点类型</returns>
            public static decimal? DataToDecimalMark(this object obj)
            {
                if (obj == null || obj == DBNull.Value)
                {
                    return null;
                }
    
                decimal data = decimal.MinValue;
                if (decimal.TryParse(obj.ToString(), out data) == false)
                {
                    return null;
                }
    
                return data;
            }
    
            /// <summary>
            /// 对象转换成日期类型
            /// </summary>
            /// <param name="obj">对象</param>
            /// <returns>日期</returns>
            public static DateTime DataToDateTime(this object obj)
            {
                if (obj == null || obj == DBNull.Value)
                {
                    return DateTime.MinValue;
                }
    
                DateTime date = DateTime.MinValue;
                if (DateTime.TryParse(obj.ToString(), out date) == false)
                {
                    return date;
                }
    
                return date;
            }
    
            /// <summary>
            /// 对象转换成时间类型
            /// </summary>
            /// <param name="obj">对象</param>
            /// <returns>时间</returns>
            public static DateTime DataToTime(this object obj)
            {
                DateTime date = DateTime.ParseExact("00:00:00", "HH:mm:ss", null);
                if (obj == null || obj == DBNull.Value)
                {
                    return date;
                }
    
                if (DateTime.TryParse(obj.ToString(), out date) == false)
                {
                    date = DateTime.ParseExact(obj.ToString(), "HH:mm:ss", null);
                }
                return date;
            }
    
            /// <summary>
            /// 对象转换成日期类型
            /// </summary>
            /// <param name="obj">对象</param>
            /// <returns>日期</returns>
            public static DateTime? DataToDateTimeMark(this object obj)
            {
                if (obj == null || obj == DBNull.Value)
                {
                    return null;
                }
    
                DateTime date = DateTime.MinValue;
                if (DateTime.TryParse(obj.ToString(), out date) == false)
                {
                    return null;
                }
    
                return date;
            }
        }
    View Code
  • 相关阅读:
    UEmacs/PK Keybindings
    ubunut install golang
    vyos ipsec l2tp simple experiment
    Lynx 命令行操作
    Linux开启路由转发功能
    PROTEUS的元器件及模型制作
    5G模拟网自动化测试的需求和挑战
    vyos site-to-site ipsec simple experiment
    kubeadm 搭建 k8s 时用到的常用命令汇总
    html 特殊符号标记
  • 原文地址:https://www.cnblogs.com/ywblog/p/4243340.html
Copyright © 2020-2023  润新知