123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace LeaRun.Util.Extension
- {
- /// <summary>
- /// 类型转换扩展
- /// </summary>
- public static partial class Extensions
- {
- #region 数值转换
- /// <summary>
- /// 转换为整型
- /// </summary>
- /// <param name="data">数据</param>
- public static int ToInt(this object data)
- {
- if (data == null)
- return 0;
- int result;
- var success = int.TryParse(data.ToString(), out result);
- if (success)
- return result;
- try
- {
- return Convert.ToInt32(ToDouble(data, 0));
- }
- catch (Exception)
- {
- return 0;
- }
- }
- /// <summary>
- /// 转换为可空整型
- /// </summary>
- /// <param name="data">数据</param>
- public static int? ToIntOrNull(this object data)
- {
- if (data == null)
- return null;
- int result;
- bool isValid = int.TryParse(data.ToString(), out result);
- if (isValid)
- return result;
- return null;
- }
- /// <summary>
- /// 转换为双精度浮点数
- /// </summary>
- /// <param name="data">数据</param>
- public static double ToDouble(this object data)
- {
- if (data == null)
- return 0;
- double result;
- return double.TryParse(data.ToString(), out result) ? result : 0;
- }
- /// <summary>
- /// 转换为双精度浮点数,并按指定的小数位4舍5入
- /// </summary>
- /// <param name="data">数据</param>
- /// <param name="digits">小数位数</param>
- public static double ToDouble(this object data, int digits)
- {
- return Math.Round(ToDouble(data), digits);
- }
- /// <summary>
- /// 转换为可空双精度浮点数
- /// </summary>
- /// <param name="data">数据</param>
- public static double? ToDoubleOrNull(this object data)
- {
- if (data == null)
- return null;
- double result;
- bool isValid = double.TryParse(data.ToString(), out result);
- if (isValid)
- return result;
- return null;
- }
- /// <summary>
- /// 转换为高精度浮点数
- /// </summary>
- /// <param name="data">数据</param>
- public static decimal ToDecimal(this object data)
- {
- if (data == null)
- return 0;
- decimal result;
- return decimal.TryParse(data.ToString(), out result) ? result : 0;
- }
- /// <summary>
- /// 转换为高精度浮点数,并按指定的小数位4舍5入
- /// </summary>
- /// <param name="data">数据</param>
- /// <param name="digits">小数位数</param>
- public static decimal ToDecimal(this object data, int digits)
- {
- return Math.Round(ToDecimal(data), digits);
- }
- /// <summary>
- /// 转换为可空高精度浮点数
- /// </summary>
- /// <param name="data">数据</param>
- public static decimal? ToDecimalOrNull(this object data)
- {
- if (data == null)
- return null;
- decimal result;
- bool isValid = decimal.TryParse(data.ToString(), out result);
- if (isValid)
- return result;
- return null;
- }
- /// <summary>
- /// 转换为可空高精度浮点数,并按指定的小数位4舍5入
- /// </summary>
- /// <param name="data">数据</param>
- /// <param name="digits">小数位数</param>
- public static decimal? ToDecimalOrNull(this object data, int digits)
- {
- var result = ToDecimalOrNull(data);
- if (result == null)
- return null;
- return Math.Round(result.Value, digits);
- }
- #endregion
- #region 日期转换
- /// <summary>
- /// 转换为日期
- /// </summary>
- /// <param name="data">数据</param>
- public static DateTime ToDate(this object data)
- {
- if (data == null)
- return DateTime.MinValue;
- DateTime result;
- return DateTime.TryParse(data.ToString(), out result) ? result : DateTime.MinValue;
- }
- /// <summary>
- /// 转换为可空日期
- /// </summary>
- /// <param name="data">数据</param>
- public static DateTime? ToDateOrNull(this object data)
- {
- if (data == null)
- return null;
- DateTime result;
- bool isValid = DateTime.TryParse(data.ToString(), out result);
- if (isValid)
- return result;
- return null;
- }
- #endregion
- #region 布尔转换
- /// <summary>
- /// 转换为布尔值
- /// </summary>
- /// <param name="data">数据</param>
- public static bool ToBool(this object data)
- {
- if (data == null)
- return false;
- bool? value = GetBool(data);
- if (value != null)
- return value.Value;
- bool result;
- return bool.TryParse(data.ToString(), out result) && result;
- }
- /// <summary>
- /// 获取布尔值
- /// </summary>
- private static bool? GetBool(this object data)
- {
- switch (data.ToString().Trim().ToLower())
- {
- case "0":
- return false;
- case "1":
- return true;
- case "是":
- return true;
- case "否":
- return false;
- case "yes":
- return true;
- case "no":
- return false;
- default:
- return null;
- }
- }
- /// <summary>
- /// 转换为可空布尔值
- /// </summary>
- /// <param name="data">数据</param>
- public static bool? ToBoolOrNull(this object data)
- {
- if (data == null)
- return null;
- bool? value = GetBool(data);
- if (value != null)
- return value.Value;
- bool result;
- bool isValid = bool.TryParse(data.ToString(), out result);
- if (isValid)
- return result;
- return null;
- }
- #endregion
- #region 字符串转换
- /// <summary>
- /// 转换为字符串
- /// </summary>
- /// <param name="data">数据</param>
- public static string ToString(this object data)
- {
- return data == null ? string.Empty : data.ToString().Trim();
- }
- #endregion
- }
- }
|