|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.Linq; |
|
|
using System.Text; |
|
|
using System.Threading.Tasks; |
|
|
|
|
|
namespace IDGForNDBG |
|
|
{ |
|
|
public static class ExtendMethd |
|
|
{ |
|
|
|
|
|
public static string ToTrim(this object value) |
|
|
{ |
|
|
if (value == null || value == DBNull.Value) |
|
|
return string.Empty; |
|
|
return value.ToString().Trim(); |
|
|
} |
|
|
public static int ToInt(this object value) |
|
|
{ |
|
|
if (value == null) |
|
|
return 0; |
|
|
int result; |
|
|
int.TryParse(value.ToString(), out result); |
|
|
return result; |
|
|
} |
|
|
public static double ToDouble(this object value) |
|
|
{ |
|
|
if (value == null) |
|
|
return 0; |
|
|
double result; |
|
|
double.TryParse(value.ToString(), out result); |
|
|
return result; |
|
|
} |
|
|
|
|
|
public static double ToDouble(this object value, int w) |
|
|
{ |
|
|
if (value == null) |
|
|
return 0; |
|
|
double result; |
|
|
double.TryParse(value.ToString(), out result); |
|
|
if (result > 0) |
|
|
{ |
|
|
result = Math.Round(result, w, MidpointRounding.AwayFromZero); |
|
|
} |
|
|
return result; |
|
|
} |
|
|
/// <summary> |
|
|
/// 实现数据的四舍五入法 |
|
|
/// </summary> |
|
|
/// <param name="v">要进行处理的数据</param> |
|
|
/// <param name="x">保留的小数位数</param> |
|
|
/// <returns>四舍五入后的结果</returns> |
|
|
private static double Round(double v, int x) |
|
|
{ |
|
|
bool isNegative = false; |
|
|
//如果是负数 |
|
|
if (v < 0) |
|
|
{ |
|
|
isNegative = true; |
|
|
v = -v; |
|
|
} |
|
|
|
|
|
int IValue = 1; |
|
|
for (int i = 1; i <= x; i++) |
|
|
{ |
|
|
IValue = IValue * 10; |
|
|
} |
|
|
double Int = Math.Round(v * IValue + 0.5, 0); |
|
|
v = Int / IValue; |
|
|
|
|
|
if (isNegative) |
|
|
{ |
|
|
v = -v; |
|
|
} |
|
|
|
|
|
return v; |
|
|
} |
|
|
|
|
|
public static decimal ToDecimal(this object value) |
|
|
{ |
|
|
if (value == null) |
|
|
return 0; |
|
|
decimal result; |
|
|
decimal.TryParse(value.ToString(), out result); |
|
|
return result; |
|
|
} |
|
|
|
|
|
public static decimal ToDecimal(this object value, int w) |
|
|
{ |
|
|
if (value == null) |
|
|
return 0; |
|
|
decimal result; |
|
|
decimal.TryParse(value.ToString(), out result); |
|
|
if (result > 0) |
|
|
{ |
|
|
result = Math.Round(result, w, MidpointRounding.AwayFromZero); |
|
|
} |
|
|
return result; |
|
|
} |
|
|
|
|
|
/// <summary> |
|
|
/// 判断 Object 是否不能转化成 DateTime 型数据(判断Object是否不能转化成 DateTime 型数据) |
|
|
/// </summary> |
|
|
/// <param name="sTheValue">判断对象</param> |
|
|
/// <returns></returns> |
|
|
public static bool JudgeIsCouldConversionDateTime(object sTheValue) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
if (sTheValue == null) |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
else |
|
|
{ |
|
|
if (string.IsNullOrWhiteSpace(sTheValue.ToString()) == true) |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
else |
|
|
{ |
|
|
DateTime dTemp = new DateTime(); |
|
|
if (DateTime.TryParse(sTheValue.ToString(), out dTemp) == false) |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
else |
|
|
{ |
|
|
return true; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
/// <summary> |
|
|
/// 依据 obj获取 int |
|
|
/// </summary> |
|
|
/// <param name="obj"></param> |
|
|
/// <returns></returns> |
|
|
public static int GetIntByObject(object obj) |
|
|
{ |
|
|
int iTemp = 0; |
|
|
try |
|
|
{ |
|
|
iTemp = JudgeIsCouldConversionInt(obj) == true ? Convert.ToInt32(obj) : 0; |
|
|
return iTemp; |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
/// <summary> |
|
|
/// 判断 Object 是否不能转化成 int 型数据(判断Object是否不能转化成 int 型数据) |
|
|
/// </summary> |
|
|
/// <param name="sTheValue"></param> |
|
|
/// <returns></returns> |
|
|
public static bool JudgeIsCouldConversionInt(object sTheValue) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
if (sTheValue == null) |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
else |
|
|
{ |
|
|
if (string.IsNullOrWhiteSpace(sTheValue.ToString()) == true) |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
else |
|
|
{ |
|
|
int iTemp = 0; |
|
|
if (int.TryParse(sTheValue.ToString(), out iTemp) == false) |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
else |
|
|
{ |
|
|
return true; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static string GetStringByObject(object obj) |
|
|
{ |
|
|
string sTemp = ""; |
|
|
try |
|
|
{ |
|
|
sTemp = JudgeIsCouldConversionStringOrHasValue(obj) == true ? obj.ToString() : ""; |
|
|
return sTemp; |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
return ""; |
|
|
} |
|
|
} |
|
|
|
|
|
/// <summary> |
|
|
/// 判断 Object 是否不能转化成 String 字符串(判断Object是否不能转化成String 字符串) |
|
|
/// </summary> |
|
|
/// <param name="sTheValue"></param> |
|
|
/// <returns></returns> |
|
|
public static bool JudgeIsCouldConversionStringOrHasValue(object sTheValue) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
if (sTheValue == null) |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
else |
|
|
{ |
|
|
if (string.IsNullOrWhiteSpace(sTheValue.ToString()) == true) |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
else |
|
|
{ |
|
|
return true; |
|
|
} |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary> |
|
|
/// 判断 Object 是否不能转化成 Double (判断Object是否不能转化成Double) |
|
|
/// </summary> |
|
|
/// <param name="obj"></param> |
|
|
/// <returns></returns> |
|
|
public static bool JudgeIsCouldConversionDouble(object obj) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
if (obj == null) |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
else |
|
|
{ |
|
|
if (string.IsNullOrWhiteSpace(obj.ToString()) == true) |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
else |
|
|
{ |
|
|
double dTemp = 0; |
|
|
if (double.TryParse(obj.ToString(), out dTemp) == false) |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
else |
|
|
{ |
|
|
return true; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
}
|
|
|
|