Asp.net data type conversion code

Source: Internet
Author: User

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;
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.