首页 技术 正文
技术 2022年11月13日
0 收藏 411 点赞 3,645 浏览 4166 个字

//    /// 类型转换类    /// 处理数据库获取字段为空的情况    ///    public static class DBConvert    {        #region------------------ToInt32类型转换------------------        ///        /// 读取数据库中字符串并转换成Int32        /// 为空时返回0        ///        /// object类型的值        /// Int32类型        public static int ToInt32(object obj)        {            int result = 0;            if (IsInt(Convert.ToString(obj)))            {                result = Convert.ToInt32(obj);            }            else if (obj != null && obj is Enum) //处理非null值类型时(或者枚举)            {                result = ((IConvertible)obj).ToInt32(null);            }            return result;        }         ///        /// 读取数据库中字符串并转换成Int32        /// 为空时返回0        ///        /// string类型的值        /// Int32类型        public static int ToInt32(string str)        {            int result = 0;            if (IsInt(str))            {                result = Convert.ToInt32(str);            }            return result;        }         ///        /// 判断一个字符串是否属于Int类型        /// 如果是的返回true,如果不是返回false        ///        /// string类型的值        /// true:是Int的字符串(即可以转换成Int类型),false:不是Int类型的字符串        public static bool IsInt(string str)        {            bool result = false;            if (str != "" && str!=null)            {                Regex reg = new Regex("^[0-9]*$");                if (reg.IsMatch(str))                {                    result = true;                }            }            return result;        }        #endregion         #region------------------ToString类型转换------------------        ///        ///  读取数据库中字符串并转换成string        ///        /// object类型的值        /// string类型        public static string ToString(object obj)        {            string result = "";            if (obj != null)            {                result = Convert.ToString(obj);            }            return result;        }        #endregion         #region------------------ToDouble类型转换------------------        ///        /// 判断一个字符串是否属于Double类型(包括负浮点型)        /// 如果是的返回true,如果不是返回false        ///        /// string类型的值        /// true:是Double的字符串(即可以转换成Double类型),false:不是Double类型的字符串        public static bool IsDouble(string str)        {            bool result = false;            if (str != "" && str != null)            {                Regex reg = new Regex(@"^(-?\d+)(\.\d+)?$");                if (reg.IsMatch(str))                {                    result = true;                }            }            return result;        }         ///        /// 读取数据库中字符串并转换成Int32        /// 为空时返回0        ///        /// object类型的值        /// Int32类型        public static double ToDouble(object obj)        {            double result = 0.0;            if (IsDouble(Convert.ToString(obj)))            {                result = Convert.ToDouble(obj);            }            else if (obj != null && obj is Enum) //处理枚举            {                result = ((IConvertible)obj).ToDouble(null);            }            return result;        }         ///        /// 读取数据库中字符串并转换成Int32        /// 为空时返回0        ///        /// string类型的值        /// Int32类型        public static double ToDouble(string str)        {            double result = 0.0;            if (IsDouble(str))            {                result = Convert.ToDouble(str);            }            return result;        }        #endregion         #region------------------ToDateTime类型转换------------------        ///        /// 判断时间格式是否是时间类型        /// 如23:10        ///        /// 要判断的字符串        /// true:是时间类型的字符串(即可以转换成时间类型),false:不是时间类型的字符串        public static bool isDateTime(string str)        {            bool result = false;            if (str != "" && str != null)            {                Regex reg = new Regex("(([01]\\d)|(2[0-3])):[0-5]\\d");                if (reg.IsMatch(str))                {                    result = true;                }            }            return result;        }        #endregion     }}//"^\d+(\.\d+)?$"  //非负浮点数(正浮点数 + 0)//"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮点数//"^((-\d+(\.\d+)?)|(0+(\.0+)?))$"  //非正浮点数(负浮点数 + 0)//"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //负浮点数//"^(-?\d+)(\.\d+)?$"  //浮点数

上一篇: Hive数据提取
下一篇: GitHub:Python
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905