zoukankan      html  css  js  c++  java
  • C#:转换类型(待补充)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace MyCommanHelper
    {
        public class ConvertHelper
        {
    
        #region 方法
    
             // Methods
            public static double ObjectToDouble(object str)
            {
                try
                {
                    if (str == null)
                    {
                        return 0.0;
                    }
                    double result = 0.0;
                    double.TryParse(str.ToString(), out result);
                    return result;
                }
                catch
                {
                    return 0.0;
                }
            }
    
            public static double ObjectToDouble(string str)
            {
                try
                {
                    if ((str == "") || (str == null))
                    {
                        return 0.0;
                    }
                    double result = 0.0;
                    double.TryParse(str, out result);
                    return result;
                }
                catch
                {
                    return 0.0;
                }
            }
    
            public static float ObjectToFloat(object str)
            {
                try
                {
                    if (str == null)
                    {
                        return 0.0f;
                    }
                    float result = 0f;
                    float.TryParse(str.ToString(), out result);
                    return result;
                }
                catch
                {
                    return 0.0f;
                }
            }
    
            public static float ObjectToFloat(string str)
            {
                try
                {
                    if ((str == "") || (str == null))
                    {
                        return 0.0f;
                    }
                    float result = 0f;
                    float.TryParse(str, out result);
                    return result;
                }
                catch
                {
                    return 0.0f;
                }
            }
    
            public static int ObjectToInt(object str)
            {
                try
                {
                    if (str == null)
                    {
                        return 0;
                    }
                    int result = 0;
                    int.TryParse(str.ToString(), out result);
                    return result;
                }
                catch
                {
                    return 0;
                }
            }
    
            public static int ObjectToInt(string str)
            {
                try
                {
                    if (str == null)
                    {
                        return 0;
                    }
                    if (str == "")
                    {
                        return 0;
                    }
                    int result = 0;
                    int.TryParse(str, out result);
                    return result;
                }
                catch
                {
                    return 0;
                }
            }
    
            public static string ObjectToString(object str)
            {
                try
                {
                    if (str == null)
                    {
                        return "";
                    }
                    return Convert.ToString(str);
                }
                catch
                {
                    return "";
                }
            }
    
        #endregion
    
        }
    }
    
  • 相关阅读:
    一切都是对象
    对象入门
    同步计算输入的各个数的总和与平均值
    与时间有关的类Date,DateFormat,Calendar
    获取文件信息
    串行化
    分解
    高速缓存
    压缩
    MyCAT实现MySQL的读写分离
  • 原文地址:https://www.cnblogs.com/shenchao/p/3673225.html
Copyright © 2011-2022 走看看