欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 数据类型转换类代码

程序员文章站 2024-03-06 20:38:14
复制代码 代码如下: using system; using system.collections.generic; using system.text; using sy...
复制代码 代码如下:

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