/// <summary> /// 转换字符串为Int16类型,可以指定默认值 /// </summary> /// <param name="str">字符串内容</param> /// <param name="defaultValue">默认值</param> /// <returns></returns> public static int ToInt16(this string str, Int16 defaultValue = 0) { str = ConvertHelper.ConvertToDBC(str);//先转换为半角字符串 bool converted = Int16.TryParse(str, out defaultValue); return(defaultValue); }
/// <summary> /// 转换字符串为float类型,可以指定默认值 /// </summary> /// <param name="str">字符串内容</param> /// <returns></returns> public static bool ToBoolean(this string str) { str = ConvertHelper.ConvertToDBC(str);//先转换为半角字符串 //bool defaultValue = false; //bool converted = bool.TryParse(str, out defaultValue); //return defaultValue; bool ret = false; switch (str.ToLower()) { case "true": case "t": case "yes": case "y": case "1": case "-1": ret = true; break; case "false": case "f": case "no": case "n": case "0": ret = false; break; default: ret = false; break; } return(ret); }
/// <summary> /// 转换字符串为float类型,可以指定默认值 /// </summary> /// <param name="str">字符串内容</param> /// <param name="defaultValue">默认值</param> /// <returns></returns> public static float ToFloat(this string str, float defaultValue = 0) { str = ConvertHelper.ConvertToDBC(str);//先转换为半角字符串 bool converted = float.TryParse(str, out defaultValue); return(defaultValue); }
/// <summary> /// 转换字符串为decimal类型,可以指定默认值 /// </summary> /// <param name="str">字符串内容</param> /// <param name="defaultValue">默认值</param> /// <returns></returns> public static decimal ToDecimal(this string str, decimal defaultValue = 0M) { str = ConvertHelper.ConvertToDBC(str);//先转换为半角字符串 bool converted = decimal.TryParse(str, out defaultValue); return(defaultValue); }
/// <summary> /// 转换字符串为double类型,可以指定默认值 /// </summary> /// <param name="str">字符串内容</param> /// <param name="defaultValue">默认值</param> /// <returns></returns> public static double ToDouble(this string str, double defaultValue = 0) { str = ConvertHelper.ConvertToDBC(str);//先转换为半角字符串 bool converted = double.TryParse(str, out defaultValue); return(defaultValue); }
/// <summary> /// 转换字符串为uint类型,可以指定默认值 /// </summary> /// <param name="str">字符串内容</param> /// <param name="defaultValue">默认值</param> /// <returns></returns> public static uint ToUInt32(this string str, uint defaultValue = 0) { str = ConvertHelper.ConvertToDBC(str);//先转换为半角字符串 bool converted = uint.TryParse(str, out defaultValue); return(defaultValue); }
/// <summary> /// 转换字符串为UInt64类型,可以指定默认值 /// </summary> /// <param name="str">字符串内容</param> /// <param name="defaultValue">默认值</param> /// <returns></returns> public static UInt64 ToUInt64(this string str, UInt64 defaultValue = 0) { str = ConvertHelper.ConvertToDBC(str);//先转换为半角字符串 bool converted = UInt64.TryParse(str, out defaultValue); return(defaultValue); }
/// <summary> /// 转换字符串为日期类型 /// </summary> /// <param name="str">字符串内容</param> /// <param name="defaultValue">默认值</param> /// <returns></returns> public static DateTime ToDateTime(this string str) { str = ConvertHelper.ConvertToDBC(str);//先转换为半角字符串 DateTime defaultValue = Convert.ToDateTime("1900-1-1"); bool converted = DateTime.TryParse(str, out defaultValue); return(defaultValue); }