C# 常用数据转换处理方法整理

                #region 数据转换
		/// <summary>
		/// 返回对象obj的String值,obj为null时返回空值。
		/// </summary>
		/// <param name="obj">对象。</param>
		/// <returns>字符串。</returns>
		public static string ToObjectString(object obj)  
		{
			return null == obj ? String.Empty : obj.ToString();
		}

		/// <summary>
		/// 将对象转换为数值(Int32)类型,转换失败返回-1。
		/// </summary>
		/// <param name="obj">对象。</param>
		/// <returns>Int32数值。</returns>
		public static int ToInt(object obj)
		{
			try
			{
				return int.Parse(ToObjectString(obj));
			}
			catch
			{ return -1; }
		}

		/// <summary>
		/// 将对象转换为数值(Int32)类型。
		/// </summary>
		/// <param name="obj">对象。</param>
		/// <param name="returnValue">转换失败返回该值。</param>
		/// <returns>Int32数值。</returns>
		public static int ToInt(object obj,int returnValue)
		{
			try
			{
				return int.Parse(ToObjectString(obj));
			}
			catch
			{ return returnValue; }
		}
		/// <summary>
		/// 将对象转换为数值(Long)类型,转换失败返回-1。
		/// </summary>
		/// <param name="obj">对象。</param>
		/// <returns>Long数值。</returns>
		public static long ToLong(object obj)
		{
			try
			{
				return long.Parse(ToObjectString(obj));
			}
			catch
			{ return -1L; }
		}
		/// <summary>
		/// 将对象转换为数值(Long)类型。
		/// </summary>
		/// <param name="obj">对象。</param>
		/// <param name="returnValue">转换失败返回该值。</param>
		/// <returns>Long数值。</returns>
		public static long ToLong(object obj,long returnValue)
		{
			try
			{
				return long.Parse(ToObjectString(obj));
			}
			catch
			{ return returnValue; }
		}
		/// <summary>
		/// 将对象转换为数值(Decimal)类型,转换失败返回-1。
		/// </summary>
		/// <param name="obj">对象。</param>
		/// <returns>Decimal数值。</returns>
		public static decimal ToDecimal(object obj)
		{
			try
			{
				return decimal.Parse(ToObjectString(obj));
			}
			catch
			{ return -1M; }
		}

		/// <summary>
		/// 将对象转换为数值(Decimal)类型。
		/// </summary>
		/// <param name="obj">对象。</param>
		/// <param name="returnValue">转换失败返回该值。</param>
		/// <returns>Decimal数值。</returns>
		public static decimal ToDecimal(object obj,decimal returnValue)
		{
			try
			{
				return decimal.Parse(ToObjectString(obj));
			}
			catch
			{ return returnValue; }
		}
		/// <summary>
		/// 将对象转换为数值(Double)类型,转换失败返回-1。
		/// </summary>
		/// <param name="obj">对象。</param>
		/// <returns>Double数值。</returns>
		public static double ToDouble(object obj)
		{
			try
			{
				return double.Parse(ToObjectString(obj));
			}
			catch
			{ return -1; }
		}

		/// <summary>
		/// 将对象转换为数值(Double)类型。
		/// </summary>
		/// <param name="obj">对象。</param>
		/// <param name="returnValue">转换失败返回该值。</param>
		/// <returns>Double数值。</returns>
		public static double ToDouble(object obj,double returnValue)
		{
			try
			{
				return double.Parse(ToObjectString(obj));
			}
			catch
			{ return returnValue; }
		}
		/// <summary>
		/// 将对象转换为数值(Float)类型,转换失败返回-1。
		/// </summary>
		/// <param name="obj">对象。</param>
		/// <returns>Float数值。</returns>
		public static float ToFloat(object obj)
		{
			try
			{
				return float.Parse(ToObjectString(obj));
			}
			catch
			{ return -1; }
		}

		/// <summary>
		/// 将对象转换为数值(Float)类型。
		/// </summary>
		/// <param name="obj">对象。</param>
		/// <param name="returnValue">转换失败返回该值。</param>
		/// <returns>Float数值。</returns>
		public static float ToFloat(object obj,float returnValue)
		{
			try
			{
				return float.Parse(ToObjectString(obj));
			}
			catch
			{ return returnValue; }
		}
		/// <summary>
		/// 将对象转换为数值(DateTime)类型,转换失败返回Now。
		/// </summary>
		/// <param name="obj">对象。</param>
		/// <returns>DateTime值。</returns>
		public static DateTime ToDateTime(object obj)
		{
			try
			{
				DateTime dt = DateTime.Parse(ToObjectString(obj));
				if( dt > DateTime.MinValue && DateTime.MaxValue > dt)
					return dt;
				return DateTime.Now;
			}
			catch
			{ return DateTime.Now; }
		}

		/// <summary>
		/// 将对象转换为数值(DateTime)类型。
		/// </summary>
		/// <param name="obj">对象。</param>
		/// <param name="returnValue">转换失败返回该值。</param>
		/// <returns>DateTime值。</returns>
		public static DateTime ToDateTime(object obj,DateTime returnValue)
		{
			try
			{
				DateTime dt = DateTime.Parse(ToObjectString(obj));
				if( dt > DateTime.MinValue && DateTime.MaxValue > dt)
					return dt;
				return returnValue;
			}
			catch
			{ return returnValue; }
		}
		/// <summary>
		/// 从Boolean转换成byte,转换失败返回0。
		/// </summary>
		/// <param name="obj">对象。</param>
		/// <returns>Byte值。</returns>
		public static byte ToByteByBool( object obj )
		{
			string text = ToObjectString(obj).Trim();
			if( text == string.Empty)
				return 0;
			else
			{
				try
				{
					return (byte)(text.ToLower()=="true"?1:0);
				}
				catch
				{
					return 0;
				}
			}
		}
		
		/// <summary>
		/// 从Boolean转换成byte。
		/// </summary>
		/// <param name="obj">对象。</param>
		/// <param name="returnValue">转换失败返回该值。</param>
		/// <returns>Byte值。</returns>
		public static byte ToByteByBool( object obj, byte returnValue )
		{
			string text = ToObjectString(obj).Trim();
			if( text == string.Empty)
				return returnValue;
			else
			{
				try
				{
					return (byte)(text.ToLower()=="true"?1:0);
				}
				catch
				{
					return returnValue;
				}
			}
		}
		/// <summary>
		/// 从byte转换成Boolean,转换失败返回false。
		/// </summary>
		/// <param name="obj">对象。</param>
		/// <returns>Boolean值。</returns>
		public static bool ToBoolByByte( object obj )
		{
			try
			{
				string s = ToObjectString(obj).ToLower();
				return s == "1" || s== "true"?true:false;
			}
			catch
			{
				return false;
			}
		}
		/// <summary>
		/// 从byte转换成Boolean。
		/// </summary>
		/// <param name="obj">对象。</param>
		/// <param name="returnValue">转换失败返回该值。</param>
		/// <returns>Boolean值。</returns>
		public static bool ToBoolByByte( object obj, bool returnValue )
		{
			try
			{
				string s = ToObjectString(obj).ToLower();
				return s == "1" || s== "true"?true:false;
			}
			catch
			{
				return returnValue;
			}
		}
		#endregion

欢迎分享,(木庄网络博客交流QQ群:562366239

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论