zoukankan      html  css  js  c++  java
  • C# 各种类型的转换

     /// <summary>
        /// 一些常用的方法
        /// 1、一些高效的转换方法
        /// </summary>
        public class Util
        {
            #region Object转换为Int32 
            /// <summary>
            /// Object转换为Int32 
            /// </summary>
            /// <param name="o">Object</param>
            /// <returns>int 报错也返回0</returns>
            public static int ConvertToInt32(object o)
            {
                try
                {
                    if (o != DBNull.Value && o != null && o.ToString() != String.Empty)
                    {
                        if (o is int)
                            return (int)o;
                        else if (o is short)
                            return (int)(short)o;
                        else if (o is byte)
                            return (int)(byte)o;
                        else if (o is long)
                            return (int)(long)o;
                        else if (o is double)
                            return (int)(double)o;
                        else if (o is float)
                            return (int)(float)o;
                        else if (o is decimal)
                            return (int)(decimal)o;
                        else if (o is uint)
                            return (int)(uint)o;
                        else if (o is ushort)
                            return (int)(ushort)o;
                        else if (o is ulong)
                            return (int)(ulong)o;
                        else if (o is sbyte)
                            return (int)(sbyte)o;
                        else
                            return int.Parse(o.ToString());
                    }
                    else
                    {
                        return 0;
                    }
                }
                catch
                {
                    return 0;
                }
    
            }
            #endregion
    
            #region Object 转成 String 
            /// <summary>
            /// Object 转成 String 
            /// </summary>
            /// <param name="o">参数</param>
            /// <returns>String</returns>
            public static string ConvertToString(object o)
            {
                try
                {
                    if (o != DBNull.Value && o != null && o.ToString() != String.Empty)
                    {
                        return o.ToString();
                    }
                    else
                    {
                        return "";
                    }
                }
                catch
                {
                    return "";
                }
            }
            #endregion
    
            #region Object转换为Decimal  
            /// <summary>
            /// Object转换为Decimal 
            /// </summary>
            /// <param name="o">Object</param>
            /// <returns>Decimal</returns>
            public static decimal ConvertToDecimal(object o)
            {
                try
                {
                    if (o != DBNull.Value && o != null && o.ToString() != String.Empty)
                    {
                        decimal Num = 0;
                        decimal.TryParse(o.ToString(), out Num);
                        return Num;
                    }
                    else
                    {
                        return 0;
                    }
                }
                catch
                {
                    return 0;
                }
    
            }
            #endregion
    
            #region Object转换为Double 
            /// <summary>
            /// Object转换为Double  
            /// </summary>
            /// <param name="o">Object</param>
            /// <returns>Double</returns>
            public static double ConvertToDouble(object o)
            {
                try
                {
                    if (o != DBNull.Value && o != null && o.ToString() != String.Empty)
                    {
                        double Num = 0;
                        double.TryParse(o.ToString(), out Num);
                        return Num;
                    }
                    else
                    {
                        return 0;
                    }
                }
                catch
                {
                    return 0;
                }
    
            }
            #endregion
    
            #region Object转换为Float 
            /// <summary>
            /// Object转换为Float  
            /// </summary>
            /// <param name="o">Object</param>
            /// <returns>Double</returns>
            public static Double ConvertToFloat(object o)
            {
                try
                {
                    if (o != DBNull.Value && o != null && o.ToString() != String.Empty)
                    {
                        float Num = 0;
                        float.TryParse(o.ToString(), out Num);
                        return Num;
                    }
                    else
                    {
                        return 0;
                    }
                }
                catch
                {
                    return 0;
                }
    
            }
            #endregion
    
        
        }
    

      

  • 相关阅读:
    gateway调用Fegin失败问题解决
    JVM调试命令简介
    oracle10g登录em后,提示“java.lang.Exception: Exception in sending Request :: null”
    vs2019中让ashx 文件中折叠收起代码
    oracle多表视图不自动更新,手动刷新视图获得基本表的数据
    可以对表单中的隐藏字段进行操纵 问题参数 __VIEWSTATEGENERATOR
    SQL Server 检测到基于一致性的逻辑 I/O 错误 页撕裂
    WIN2016和WIN10关闭同步主机服务,节省磁盘频繁读取,并关闭自动维护
    IIS网站应用程序配置不继承网站.net框架版本
    c#web错误码CS0227,不安全代码只会在使用/unsafe编译情况下出现
  • 原文地址:https://www.cnblogs.com/LoveTX/p/5683508.html
Copyright © 2011-2022 走看看