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

    
    }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏闵开慧

Failed to set permissions of path

12/09/04 16:46:34 INFO support.ClassPathXmlApplicationContext: Refreshing org...

2707
来自专栏增长技术

App Guide相关

##TourGuide https://github.com/worker8/TourGuide

702
来自专栏一个会写诗的程序员的博客

java.base.jmod

/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/jmods$ jmod list java....

1112
来自专栏祝威廉

Spark Streaming Dynamic Resource Allocation

DRA has already been implemented since Spark 1.2 . However the existing Spark DR...

1383
来自专栏MelonTeam专栏

Bitmap 源码阅读笔记

导语: Android 系统上的图片的处理,跟Bitmap 这个类脱不了关系,我们有必要去深入阅读里面的源码,以便在工作中能更好的处理Bitmap相关的问题...

2458
来自专栏Golang语言社区

Knapsack problem algorithms for my real-life carry-on knapsack

I'm a nomad and live out of one carry-on bag. This means that the total weight o...

1142
来自专栏搞前端的李蚊子

Html5模拟通讯录人员排序(sen.js)

// JavaScript Document  var PY_Json_Str = ""; var PY_Str_1 = ""; var PY_Str_...

5866
来自专栏运维

linux下连接windows2003 ppoe 服务器

1,在CentOS6.3下用 /usr/sbin/pppd  pty "/usr/sbin/pptp publicip --nolaunchpppd" f...

712
来自专栏cloudskyme

java.lang.IllegalArgumentException: Wrong FS: hdfs://localhost:54310/user/Hadoop/b, expected: file:/

 java.lang.IllegalArgumentException: Wrong FS: hdfs://localhost:54310/user/Hadoo...

3507
来自专栏linux驱动个人学习

高通msm8909耳机调试

1、DTS相应修改: DTS相关代码:kernel/arch/arm/boot/dts/qcom/msm8909-qrd-skuc.dtsi: 1 s...

7385

扫码关注云+社区