字符串类型转换

代码
//字符串转数字
public static int StrToInt(string str, int defValue)
{
int num;
if (int.TryParse(str, out num))
{
return num;
}
return defValue;
}
public static int StrToInt(string str)
{
return TypeConvert.StrToInt(str, 0);
}
//字符串转换为时间
public static DateTime StrToDateTime(string str, DateTime defValue)
{
DateTime time;
if (DateTime.TryParse(str, out time))
{
return time;
}
return defValue;
}
public static DateTime StrToDateTime(string str)
{
return TypeConvert.StrToDateTime(str, DateTime.Now);
}
//字符串转double
public static double StrToDouble(string str, double defValue)
{
double res;
if (double.TryParse(str, out res))
{
return res;
}
return defValue;
}
public static double StrToDouble(string str)
{
return TypeConvert.StrToDouble(str, 0f);
}
//object转数字
public static int ObjectToInt(object obj, int defValue)
{
if (obj != null)
{
return TypeConvert.StrToInt(obj.ToString(), defValue);
}
return defValue;
}
public static int ObjectToInt(object obj)
{
return TypeConvert.ObjectToInt(obj, 0);
}
//object转时间
public static DateTime ObjectToDateTime(object obj, DateTime defValue)
{
if (obj != null)
{
return TypeConvert.StrToDateTime(obj.ToString(), defValue);
}
return defValue;
}
public static DateTime ObjectToDateTime(object obj)
{
return TypeConvert.ObjectToDateTime(obj, DateTime.Now);
}
//object转换为double
public static double ObjectToDouble(object obj, double defValue)
{
if (obj != null)
{
return TypeConvert.StrToDouble(obj.ToString(), defValue);
}
return defValue;
}
public static double ObjectToDouble(object obj)
{
return TypeConvert.ObjectToDouble(obj, 0f);
}
//object转string
public static string ObjectToString(object obj, string defValue)
{
if (obj == null)
{
return defValue;
}
return obj.ToString();
}
public static String ObjectToString(object obj)
{
return TypeConvert.ObjectToString(obj, "");
}

 

posted @ 2011-01-12 15:53  infi  阅读(339)  评论(0编辑  收藏  举报