387 lines
12 KiB
C#
387 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace JinYuan.Helper
|
|
{
|
|
public class TypeParse
|
|
{
|
|
/// <summary>
|
|
/// 将数据库内取的值转成字符串
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public static string Obj2Str(object obj)
|
|
{
|
|
return (obj != null) ? obj.ToString().Trim() : "";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断对象是否为Int32类型的数字
|
|
/// </summary>
|
|
/// <param name="Expression"></param>
|
|
/// <returns></returns>
|
|
public static bool IsNumeric(object expression)
|
|
{
|
|
if (expression != null)
|
|
{
|
|
return IsNumeric(expression.ToString());
|
|
}
|
|
return false;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断对象是否为Int32类型的数字
|
|
/// </summary>
|
|
/// <param name="Expression"></param>
|
|
/// <returns></returns>
|
|
public static bool IsNumeric(string expression)
|
|
{
|
|
if (expression != null)
|
|
{
|
|
string str = expression;
|
|
if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
|
|
{
|
|
if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否为Double类型
|
|
/// </summary>
|
|
/// <param name="expression"></param>
|
|
/// <returns></returns>
|
|
public static bool IsDouble(object expression)
|
|
{
|
|
if (expression != null)
|
|
{
|
|
return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// string型转换为bool型
|
|
/// </summary>
|
|
/// <param name="strValue">要转换的字符串</param>
|
|
/// <param name="defValue">缺省值</param>
|
|
/// <returns>转换后的bool类型结果</returns>
|
|
public static bool StrToBool(object expression, bool defValue)
|
|
{
|
|
if (expression != null)
|
|
{
|
|
return StrToBool(expression.ToString(), defValue);
|
|
}
|
|
return defValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// string型转换为bool型
|
|
/// </summary>
|
|
/// <param name="strValue">要转换的字符串</param>
|
|
/// <param name="defValue">缺省值</param>
|
|
/// <returns>转换后的bool类型结果</returns>
|
|
public static bool StrToBool(string expression, bool defValue)
|
|
{
|
|
try
|
|
{
|
|
if (expression != null)
|
|
{
|
|
if (string.Compare(expression, "true", true) == 0)
|
|
{
|
|
return true;
|
|
}
|
|
else if (string.Compare(expression, "false", true) == 0)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return defValue;
|
|
}
|
|
return defValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将对象转换为Int32类型
|
|
/// </summary>
|
|
/// <param name="strValue">要转换的字符串</param>
|
|
/// <param name="defValue">缺省值</param>
|
|
/// <returns>转换后的int类型结果</returns>
|
|
public static int StrToInt(object expression, int defValue)
|
|
{
|
|
if (expression != null)
|
|
{
|
|
return StrToInt(expression.ToString(), defValue);
|
|
}
|
|
return defValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将对象转换为Int32类型
|
|
/// </summary>
|
|
/// <param name="str">要转换的字符串</param>
|
|
/// <param name="defValue">缺省值</param>
|
|
/// <returns>转换后的int类型结果</returns>
|
|
public static int StrToInt(string str, int defValue)
|
|
{
|
|
try
|
|
{
|
|
if (str == null)
|
|
return defValue;
|
|
str = str.Trim();
|
|
if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*$"))
|
|
{
|
|
if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
|
|
{
|
|
return Convert.ToInt32(str);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return defValue;
|
|
}
|
|
return defValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// string型转换为float型
|
|
/// </summary>
|
|
/// <param name="strValue">要转换的字符串</param>
|
|
/// <param name="defValue">缺省值</param>
|
|
/// <returns>转换后的int类型结果</returns>
|
|
public static float StrToFloat(object strValue, float defValue)
|
|
{
|
|
if ((strValue == null))
|
|
{
|
|
return defValue;
|
|
}
|
|
return StrToFloat(strValue.ToString(), defValue);
|
|
}
|
|
|
|
/// <summary>
|
|
/// string型转换为float型
|
|
/// </summary>
|
|
/// <param name="strValue">要转换的字符串</param>
|
|
/// <param name="defValue">缺省值</param>
|
|
/// <returns>转换后的int类型结果</returns>
|
|
public static float StrToFloat(string strValue, float defValue = 0)
|
|
{
|
|
|
|
if (strValue == null)
|
|
{
|
|
return defValue;
|
|
}
|
|
else if (strValue.IndexOf(".") > 10)
|
|
{
|
|
return defValue;
|
|
}
|
|
float intValue = defValue;
|
|
try
|
|
{
|
|
if (strValue != null)
|
|
{
|
|
bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
|
|
if (IsFloat)
|
|
{
|
|
intValue = Convert.ToSingle(strValue);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return defValue;
|
|
}
|
|
return intValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// object型转换为decimal型
|
|
/// </summary>
|
|
/// <param name="strValue">要转换的字符串</param>
|
|
/// <param name="defValue">缺省值</param>
|
|
/// <returns>转换后的decimal类型结果</returns>
|
|
public static decimal StrToDecimal(object strValue, decimal defValue = 0)
|
|
{
|
|
if ((strValue == null))
|
|
{
|
|
return defValue;
|
|
}
|
|
|
|
return StrToDecimal(strValue.ToString(), defValue);
|
|
}
|
|
|
|
/// <summary>
|
|
/// string型转换为decimal型
|
|
/// </summary>
|
|
/// <param name="strValue">要转换的字符串</param>
|
|
/// <param name="defValue">缺省值</param>
|
|
/// <returns>转换后的decimal类型结果</returns>
|
|
public static decimal StrToDecimal(string strValue, decimal defValue)
|
|
{
|
|
if (strValue == null || strValue == "")
|
|
{
|
|
return defValue;
|
|
}
|
|
|
|
decimal intValue = defValue;
|
|
try
|
|
{
|
|
if (strValue != null)
|
|
{
|
|
bool IsDecimal = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
|
|
if (IsDecimal)
|
|
{
|
|
intValue = Convert.ToDecimal(strValue);
|
|
//int precision = 15; // 假设数据库中的精度为15
|
|
//int scale = 3; // 假设数据库中的小数位数为3
|
|
// 检查valueToInsert是否超出精确度和小数位数
|
|
if (Math.Floor(intValue * (decimal)Math.Pow(18, 3)) > (decimal)Math.Pow(18, 15 - 3))
|
|
{
|
|
intValue = -9999;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return defValue;
|
|
}
|
|
return intValue;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型
|
|
/// </summary>
|
|
/// <param name="strNumber">要确认的字符串数组</param>
|
|
/// <returns>是则返加true 不是则返回 false</returns>
|
|
public static bool IsNumericArray(string[] strNumber)
|
|
{
|
|
if (strNumber == null)
|
|
{
|
|
return false;
|
|
}
|
|
if (strNumber.Length < 1)
|
|
{
|
|
return false;
|
|
}
|
|
foreach (string id in strNumber)
|
|
{
|
|
if (!IsNumeric(id))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将数据库内取的值转成日期
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public static DateTime Obj2DateTime(object obj)
|
|
{
|
|
try
|
|
{
|
|
if (obj != null)
|
|
{
|
|
return DateTime.Parse(obj.ToString());
|
|
}
|
|
else
|
|
{
|
|
return Convert.ToDateTime("1900-1-1 00:00:00");
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
return Convert.ToDateTime("1900-1-1 00:00:00");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断字符串是否是yyyy-mm-dd字符串
|
|
/// </summary>
|
|
/// <param name="str">待判断字符串</param>
|
|
/// <returns>判断结果</returns>
|
|
public static bool IsDateString(string str)
|
|
{
|
|
bool formatPassed = Regex.IsMatch(str, @"(\d{4})-(\d{1,2})-(\d{1,2})");
|
|
|
|
if (!formatPassed)
|
|
return false;
|
|
|
|
DateTime date;
|
|
|
|
return DateTime.TryParse(str, out date);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断字符串是否是 HH:mm 或者 HH:mm:ss 格式
|
|
/// </summary>
|
|
/// <param name="str">待判断字符串</param>
|
|
/// <param name="includeSecond">是否包含秒</param>
|
|
/// <returns>判断结果</returns>
|
|
public static bool IsTimeString(string str, bool includeSecond)
|
|
{
|
|
string patten = @"([0-1][0-9]|2[0-3]):([0-5][0-9])";
|
|
|
|
if (includeSecond)
|
|
patten = @"([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])";
|
|
|
|
bool formatPassed = Regex.IsMatch(str, patten);
|
|
|
|
return formatPassed;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 日期转换成unix时间戳
|
|
/// </summary>
|
|
/// <param name="dateTime"></param>
|
|
/// <returns></returns>
|
|
public static long DateTimeToUnixTimestamp(DateTime dateTime)
|
|
{
|
|
DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, dateTime.Kind);
|
|
return Convert.ToInt64((dateTime - start).TotalSeconds);
|
|
}
|
|
|
|
/// <summary>
|
|
/// unix时间戳转换成日期
|
|
/// </summary>
|
|
/// <param name="timeStamp">时间戳(秒)</param>
|
|
/// <returns></returns>
|
|
public static DateTime UnixTimestampToDateTime(DateTime target, long timeStamp)
|
|
{
|
|
DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, target.Kind);
|
|
return start.AddSeconds(timeStamp);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 日期转换成unix时间戳(13位精确到毫秒)
|
|
/// </summary>
|
|
/// <param name="dateTime"></param>
|
|
/// <returns></returns>
|
|
public static long DateTimeToUnixTimestamp_Ms(DateTime dateTime)
|
|
{
|
|
DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
|
|
|
return Convert.ToInt64(dateTime.Subtract(start).TotalMilliseconds);
|
|
}
|
|
}
|
|
}
|