类型帮助类TypeHelper

public class TypeHelper
{
   #region 转Int
   ///<summary>
   ///将string类型转换为interesting类型
   ///<summary>
   ///<param name="s">目标字符串</param>
   /// <param name="defaultValue">默认值</param>
   public static int StringToInt(string s,int defaultValue)
   {
     //如果传入的字符串不是空
     if(!string.IsNullOrWhiteSpace(s))
     {
        int result;
        //将数字的字符串转换为与他等效的32位有符号整数,成功返回true
        if(int.TryParse(s,out result))
            return result;
     }
     return defaultValue;
   }
   public static int StringToInt(string s)
   {
       return StringToInt(s,0);
   }
   public static int ObjectToInt(object o,int defaultValue)
   {
       if(o!=null)
       {
         return StringToInt(o.ToString(),defaultValue);
       }
       return defaultValue;
   }
   public static int ObjectToInt(object o)
   {
      return ObjectToInt(o,0);
   }
   #endregion
   #region转bool
   public static bool StringToBool(string s,bool defaultValue)
   {
      if(s=="false")
         return false;
      else if(s=="true")
         return true;
      return defaultValue; 
   }
   public static bool ToBool(string s)
   {
      return StringToBool(s,false);
   }
   public static bool ObjectToBool(object o,bool defaultValue) 
   {
     if(o!=null)
         return StringToBool(o.ToString(),defaultValue);
     return defaultValue;
   }
   public static bool ObjectToBool(object o)
   {
      return ObjectToBool(o,false); 
   }
   #endregion
   #region转DateTime
   public static DateTime StringToDateTime(string s,DateTime defaultValue)
   {
      if(!string.IsNullOrWhiteSpace(s))
      {
        DateTime result;
        if(DateTime.TryParse(s,out result))
             return result;
      }
      return defaultValue;
   }
   public static DateTime StringToDateTime(string s)
   {
     return StringToDateTime(s,DateTime.Now);
   }
   public static DateTime ObjectToDateTime(object o,DateTime defaultValue)
   { 
     if(o!=null)
     {
       return StringToDateTime(o.Tostring(),defaultValue)
     }
     return defaultValue;
   }
   public static DateTiem ObjectToDateTime(object o)
   {
      return ObjectToDateTime(o,DateTime.Now);
   }
   #endregion 
   #region
   public static DateTime ObjectToDecimal(string s,decimal defaultValue) 
   {
     if(!string.IsNullOrWhiteSpace(s))
     {
       decimal result;
       if(decimal.TryParse(s,out result))
         return result;
     }
     return defaultValue;
   }
    public static decimal StringToDecimal(string s)
        {
            return StringToDecimal(s, 0m);
        }
    public static decimal ObjectToDecimal(object o, decimal defaultValue)
        {
            if (o != null)
                return StringToDecimal(o.ToString(), defaultValue);

            return defaultValue;
        }
     public static decimal ObjectToDecimal(object o)
        {
            return ObjectToDecimal(o, 0m);
        }
   #endregion
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值