556 lines
16 KiB
C#
556 lines
16 KiB
C#
using System;
|
|
using System.Data.SqlTypes;
|
|
|
|
namespace Nirvana.Common
|
|
{
|
|
public static partial class Ext
|
|
{
|
|
#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>
|
|
/// <returns></returns>
|
|
public static long ToLong(this object data)
|
|
{
|
|
if (data == null)
|
|
return 0;
|
|
long result;
|
|
return long.TryParse(data.ToString(), out result) ? result : 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>
|
|
/// 转换为float
|
|
/// </summary>
|
|
/// <param name="data">数据</param>
|
|
/// <returns></returns>
|
|
public static float ToFloat(this object data)
|
|
{
|
|
if (data == null)
|
|
return 0;
|
|
float result;
|
|
return float.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)
|
|
{
|
|
var result = ToDouble(data);
|
|
string numToString = result.ToString();
|
|
int index = numToString.IndexOf(".");
|
|
int length = numToString.Length;
|
|
if (index != -1)
|
|
{
|
|
numToString = string.Format("{0}.{1}",
|
|
numToString.Substring(0, index),
|
|
numToString.Substring(index + 1, Math.Min(length - index - 1, digits)));
|
|
}
|
|
return ToDouble(numToString);
|
|
}
|
|
|
|
/// <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;
|
|
string val = data.ToString().Replace(" ", "");
|
|
return decimal.TryParse(val, 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)
|
|
{
|
|
var result = ToDecimal(data);
|
|
string numToString = result.ToString();
|
|
int index = numToString.IndexOf(".");
|
|
int length = numToString.Length;
|
|
if (index != -1)
|
|
{
|
|
numToString = string.Format("{0}.{1}",
|
|
numToString.Substring(0, index),
|
|
numToString.Substring(index + 1, Math.Min(length - index - 1, digits)));
|
|
}
|
|
return ToDecimal(numToString);
|
|
//return Math.Round(ToDecimal(numToString), digits);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为高精度浮点数,并按指定的小数位4舍5入
|
|
/// </summary>
|
|
/// <param name="data">数据</param>
|
|
/// <param name="digits">小数位数</param>
|
|
public static decimal ToDecimal45(this object data, int digits)
|
|
{
|
|
//var result = ToDecimal(data);
|
|
//string numToString = result.ToString();
|
|
//int index = numToString.IndexOf(".");
|
|
//int length = numToString.Length;
|
|
//if (index != -1)
|
|
//{
|
|
// numToString = string.Format("{0}.{1}",
|
|
// numToString.Substring(0, index),
|
|
// numToString.Substring(index + 1, Math.Min(length - index - 1, digits)));
|
|
//}
|
|
//return ToDecimal(numToString);
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 时间格式化,带时分,如果同一年则不显示年,否则显示年份
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
/// <returns></returns>
|
|
public static string ToYearDateTime(this object data)
|
|
{
|
|
if (data == null)
|
|
return string.Empty;
|
|
DateTime result;
|
|
bool isValid = DateTime.TryParse(data.ToString(), out result);
|
|
if (!isValid)
|
|
return string.Empty;
|
|
if (result == SqlDateTime.MinValue)
|
|
{
|
|
return "-";
|
|
}
|
|
if (result.Year == DateTime.Now.Year)
|
|
return result.ToString("M-d HH:mm");
|
|
return result.ToString("yyyy-M-d HH:mm");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 时间格式化,带时分,如果同一年则不显示年,否则显示年份
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
/// <returns></returns>
|
|
public static string ToYearDateTimes(this object data)
|
|
{
|
|
if (data == null)
|
|
return string.Empty;
|
|
DateTime result;
|
|
bool isValid = DateTime.TryParse(data.ToString(), out result);
|
|
if (!isValid)
|
|
return string.Empty;
|
|
if (result.Year == DateTime.Now.Year)
|
|
return result.ToString("MM-dd HH:mm");
|
|
return result.ToString("yyyy-MM-dd HH:mm");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 时间格式化,不带时分,如果同一年则不显示年,否则显示年份
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
/// <returns></returns>
|
|
public static string ToYearDate(this object data)
|
|
{
|
|
if (data == null)
|
|
return string.Empty;
|
|
DateTime result;
|
|
bool isValid = DateTime.TryParse(data.ToString(), out result);
|
|
if (!isValid)
|
|
return string.Empty;
|
|
if (result.Year == DateTime.Now.Year)
|
|
return result.ToString("M-d");
|
|
return result.ToString("yyyy-M-d");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 时间格式化,只有年月,如果同一年则不显示年,否则显示年份
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
/// <returns></returns>
|
|
public static string ToYearMonth(this object data)
|
|
{
|
|
if (data == null)
|
|
return string.Empty;
|
|
DateTime result;
|
|
bool isValid = DateTime.TryParse(data.ToString(), out result);
|
|
if (!isValid)
|
|
return string.Empty;
|
|
if (result.Year == DateTime.Now.Year)
|
|
return result.ToString("M");
|
|
return result.ToString("yyyy-M");
|
|
}
|
|
|
|
#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 Guid转换
|
|
|
|
/// <summary>
|
|
/// 转换为Guid
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
/// <returns></returns>
|
|
public static Guid ToGuid(this object data)
|
|
{
|
|
if (data == null)
|
|
{
|
|
return Guid.Empty;
|
|
}
|
|
Guid result;
|
|
bool isValid = Guid.TryParse(data.ToString(), out result);
|
|
if (isValid)
|
|
return result;
|
|
return Guid.Empty;
|
|
}
|
|
|
|
#endregion Guid转换
|
|
|
|
#region 字符串转换
|
|
|
|
/// <summary>
|
|
/// 转换为字符串
|
|
/// </summary>
|
|
/// <param name="data">数据</param>
|
|
public static string ToString(this object data)
|
|
{
|
|
return data == null ? string.Empty : data.ToString().Trim();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 针对可能为空的字符串处理
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
/// <returns></returns>
|
|
public static string ToStr(this string data)
|
|
{
|
|
return string.IsNullOrWhiteSpace(data) ? string.Empty : data.Trim();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 针对可能为空的字符串处理
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
/// <returns></returns>
|
|
public static string ToStrNoEmpty(this string data)
|
|
{
|
|
return string.IsNullOrWhiteSpace(data) ? string.Empty : data.Replace(" ", "");
|
|
}
|
|
|
|
#endregion 字符串转换
|
|
|
|
#region 进制转换
|
|
|
|
/// <summary>
|
|
/// 字节数组转16进制
|
|
/// </summary>
|
|
/// <param name="bt"></param>
|
|
/// <returns></returns>
|
|
public static string BytesToHexStr(this byte[] bt)
|
|
{
|
|
string returnStr = "";
|
|
if (bt != null)
|
|
{
|
|
for (int i = 0; i < bt.Length; i++)
|
|
{
|
|
returnStr += bt[i].ToString("X2") + " ";
|
|
}
|
|
}
|
|
return returnStr;
|
|
}
|
|
|
|
/// <summary>
|
|
/// byte转int
|
|
/// </summary>
|
|
/// <param name="bt"></param>
|
|
/// <returns></returns>
|
|
public static int ByteToInt(this byte bt)
|
|
{
|
|
return Convert.ToInt32(((int)bt).ToString("X2"), 16);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 16进制转10进制
|
|
/// </summary>
|
|
/// <param name="hexStr"></param>
|
|
/// <returns></returns>
|
|
public static int HexToDesc(this string hexStr)
|
|
{
|
|
return Convert.ToInt32(hexStr, 16);
|
|
}
|
|
|
|
///<summary>
|
|
/// 字节转16进制
|
|
/// </summary>
|
|
/// <param name="bt"></param>
|
|
/// <returns></returns>
|
|
public static string ByteToHexStr(this byte bt)
|
|
{
|
|
return ((int)bt).ToString("X2");
|
|
}
|
|
|
|
#endregion 进制转换
|
|
|
|
/// <summary>
|
|
/// 安全返回值
|
|
/// </summary>
|
|
/// <param name="value">可空值</param>
|
|
public static T SafeValue<T>(this T? value) where T : struct
|
|
{
|
|
return value ?? default(T);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否为空
|
|
/// </summary>
|
|
/// <param name="value">值</param>
|
|
public static bool IsEmpty(this string value)
|
|
{
|
|
return string.IsNullOrWhiteSpace(value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否为空
|
|
/// </summary>
|
|
/// <param name="value">值</param>
|
|
public static bool IsEmpty(this Guid? value)
|
|
{
|
|
if (value == null)
|
|
return true;
|
|
return IsEmpty(value.Value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否为空
|
|
/// </summary>
|
|
/// <param name="value">值</param>
|
|
public static bool IsEmpty(this Guid value)
|
|
{
|
|
if (value == Guid.Empty)
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否为空
|
|
/// </summary>
|
|
/// <param name="value">值</param>
|
|
public static bool IsEmpty(this object value)
|
|
{
|
|
if (value != null && !string.IsNullOrEmpty(value.ToString()))
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否为空
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public static bool IsEmpty(this DateTime? value)
|
|
{
|
|
if (value != null && value.HasValue)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
} |