| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- using System;
- using System.Text.RegularExpressions;
- namespace CallCenter.Utility
- {
- /// <summary>
- /// 数据转换操作类
- /// </summary>
- public class TypeConverter
- {
- /// <summary>
- /// string型转换为bool型
- /// </summary>
- /// <param name="expression">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的bool类型结果</returns>
- public static bool StrToBool(object expression, bool defValue)
- {
- if (expression != null)
- return StrToBool(expression, defValue);
- return defValue;
- }
- /// <summary>
- /// string型转换为bool型
- /// </summary>
- /// <param name="expression">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的bool类型结果</returns>
- public static bool StrToBool(string expression, bool defValue)
- {
- if (expression != null)
- {
- if (String.Compare(expression, "true", StringComparison.OrdinalIgnoreCase) == 0)
- return true;
- if (String.Compare(expression, "false", StringComparison.OrdinalIgnoreCase) == 0)
- return false;
- }
- return defValue;
- }
- /// <summary>
- /// 将对象转换为Int32类型
- /// </summary>
- /// <param name="expression">要转换的字符串</param>
- /// <returns>转换后的int类型结果</returns>
- public static int ObjectToInt(object expression)
- {
- return ObjectToInt(expression, 0);
- }
- /// <summary>
- /// 将对象转换为Int32类型
- /// </summary>
- /// <param name="expression">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static int ObjectToInt(object expression, int defValue)
- {
- if (expression != null)
- return StrToInt(expression.ToString(), defValue);
- return defValue;
- }
- /// <summary>
- /// 将对象转换为Int32类型,转换失败返回0
- /// </summary>
- /// <param name="str">要转换的字符串</param>
- /// <returns>转换后的int类型结果</returns>
- public static int StrToInt(string str)
- {
- return StrToInt(str, 0);
- }
- /// <summary>
- /// 将对象转换为Int32类型
- /// </summary>
- /// <param name="str">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static int StrToInt(string str, int defValue)
- {
- if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
- return defValue;
- int rv;
- if (Int32.TryParse(str, out rv))
- return rv;
- return Convert.ToInt32(StrToFloat(str, defValue));
- }
- /// <summary>
- /// string型转换为float型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static float StrToFloat(object strValue, float defValue)
- {
- if ((strValue == null))
- return defValue;
- return StrToFloat(strValue.ToString(), defValue);
- }
- /// <summary>
- /// string型转换为float型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static float ObjectToFloat(object strValue, float defValue)
- {
- if ((strValue == null))
- return defValue;
- return StrToFloat(strValue.ToString(), defValue);
- }
- /// <summary>
- /// string型转换为float型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <returns>转换后的int类型结果</returns>
- public static float ObjectToFloat(object strValue)
- {
- return ObjectToFloat(strValue.ToString(), 0);
- }
- /// <summary>
- /// string型转换为float型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <returns>转换后的int类型结果</returns>
- public static float StrToFloat(object strValue)
- {
- if ((strValue == null))
- return 0;
- return StrToFloat(strValue.ToString(), 0);
- }
- /// <summary>
- /// string型转换为float型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static float StrToFloat(string strValue, float defValue)
- {
- if ((strValue == null) || (strValue.Length > 10))
- return defValue;
- float intValue = defValue;
- bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
- if (IsFloat)
- float.TryParse(strValue, out intValue);
- return intValue;
- }
- }
- }
|