Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Text. RegularExpressions;
Namespace TypeClass
{
Public class TypeParse
{
/// <Summary>
/// Determine whether the object is an Int32 number
/// </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>
/// Convert string type to bool type
/// </Summary>
/// <Param name = "strValue"> string to be converted </param>
/// <Param name = "defValue"> default value </param>
/// <Returns> converted bool type result </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>
/// Convert the object to Int32 type
/// </Summary>
/// <Param name = "strValue"> string to be converted </param>
/// <Param name = "defValue"> default value </param>
/// <Returns> converted Int32 result </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>
/// Convert string type to float type
/// </Summary>
/// <Param name = "strValue"> string to be converted </param>
/// <Param name = "defValue"> default value </param>
/// <Returns> converted float Type result </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>
/// Convert string type to Decimal type
/// </Summary>
/// <Param name = "strValue"> string to be converted </param>
/// <Param name = "defValue"> default value </param>
/// <Returns> converted Decimal type result </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>
/// Convert string type to datetime type
/// </Summary>
/// <Param name = "strValue"> string to be converted </param>
/// <Param name = "defValue"> default value </param>
/// <Returns> converted datetime type result </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>
/// Determine whether all data in the given String Array (strNumber) is Numeric.
/// </Summary>
/// <Param name = "strNumber"> string array to be confirmed </param>
/// <Returns> yes. If true is not returned, false is returned. </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;
}
}
}