複製代碼 代碼如下:using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace TypeClass
{
public class TypeParse
{
/// <summary>
/// 判斷對象是否為Int32類型的數字
/// </summary>
/// <param name="Expression"></param>
/// <returns></returns>
public static bool IsNumeric(object Expression)
{
if (Expression != null)
{
int intVal;
return int.TryParse(Expression.ToString(), out intVal);
}
return false;
}
public static bool IsDouble(object Expression)
{
if (Expression != null)
{
double doubleVal;
return double.TryParse(Expression.ToString(), out doubleVal);
}
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)
{
bool boolValue;
if (bool.TryParse(Expression.ToString(), out boolValue))
return boolValue;
else
return defValue;
}
return defValue;
}
/// <summary>
/// 將對象轉換為Int32類型
/// </summary>
/// <param name="strValue">要轉換的字串</param>
/// <param name="defValue">預設值</param>
/// <returns>轉換後的Int32類型結果</returns>
public static int StrToInt(object Expression, int defValue)
{
if (Expression != null)
{
int intValue;
if (int.TryParse(Expression.ToString(), out intValue))
return intValue;
else
return defValue;
}
return defValue;
}
/// <summary>
/// string型轉換為float型
/// </summary>
/// <param name="strValue">要轉換的字串</param>
/// <param name="defValue">預設值</param>
/// <returns>轉換後的float類型結果</returns>
public static float StrToFloat(object strValue, float defValue)
{
if (strValue != null)
{
float floatValue;
if (float.TryParse(strValue.ToString(), out floatValue))
return floatValue;
else
return defValue;
}
return defValue;
}
/// <summary>
/// string型轉換為Decimal型
/// </summary>
/// <param name="strValue">要轉換的字串</param>
/// <param name="defValue">預設值</param>
/// <returns>轉換後的Decimal類型結果</returns>
public static Decimal StrToDecimal(object strValue, Decimal defValue)
{
if (strValue != null)
{
Decimal decimalValue;
if (Decimal.TryParse(strValue.ToString(), out decimalValue))
return Math.Round(decimalValue,2);
else
return defValue;
}
return defValue;
}
/// <summary>
/// string型轉換為datetime型
/// </summary>
/// <param name="strValue">要轉換的字串</param>
/// <param name="defValue">預設值</param>
/// <returns>轉換後的datetime類型結果</returns>
public static DateTime StrToDateTime(object strValue, DateTime defValue)
{
if (strValue != null)
{
DateTime DateTimeValue;
if (DateTime.TryParse(strValue.ToString(), out DateTimeValue))
return DateTimeValue;
else
return defValue;
}
return defValue;
}
/// <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;
}
}
}