zoukankan      html  css  js  c++  java
  • Web--TypeConverter

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    
    namespace Xima.FreamWork.Common.Web
    {
        public class TypeConverter
        {
    
            /// <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)
                    return StrToBool(expression, defValue);
    
                return defValue;
            }
    
            /// <summary>
            /// string型转换为bool型
            /// </summary>
            /// <param name="strValue">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的bool类型结果</returns>
            public static bool StrToBool(string expression, bool defValue)
            {
                if (expression != null)
                {
                    if (string.Compare(expression, "true", true) == 0 || string.Compare(expression, "on", true) == 0)
                        return true;
                    else if (string.Compare(expression, "false", true) == 0 || string.Compare(expression, "off", true) == 0)
                        return false;
                }
                return defValue;
            }
    
    
            /// <summary>
            /// obj转换成string型
            /// </summary>
            /// <param name="expression"></param>
            /// <returns></returns>
            public static string ObjToStr(object expression)
            {
                if (expression != null)
                {
                    return Convert.ToString(expression);
                }
                return string.Empty;
            }
    
            /// <summary>
            /// 将对象转换为Int32类型
            /// </summary>
            /// <param name="strValue">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的int类型结果</returns>
            public static int ObjectToInt(object expression)
            {
                return ObjectToInt(expression, 0);
            }
    
            /// <summary>
            /// 将对象转换为Int32类型
            /// </summary>
            /// <param name="strValue">要转换的字符串</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>
            /// 将对象转换为Byte类型
            /// </summary>
            /// <param name="expression">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的int类型结果</returns>
            public static Byte ObjectToByte(object expression, Byte defValue)
            {
                if (expression != null)
                    return StrToByte(expression.ToString(), defValue);
                return defValue;
            }
    
            /// <summary>
            /// 将对象转换为Byte类型
            /// </summary>
            /// <param name="str">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的int类型结果</returns>
            public static Byte StrToByte(string str, Byte defValue)
            {
                if (string.IsNullOrEmpty(str) || str.Trim().Length > 3 || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(.w*)?$"))
                    return defValue;
                Byte rv;
                if (Byte.TryParse(str, out rv))
                    return rv;
                return rv == 0 ? defValue : Convert.ToByte(StrToFloat(str, defValue));
            }
    
    
    
            /// <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>
            /// 将对象转换为Int32类型
            /// </summary>
            /// <param name="str">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的int类型结果</returns>
            public static Int64 StrToInt64(string str, int defValue)
            {
                if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(.w*)?$"))
                    return defValue;
    
                Int64 rv;
                if (Int64.TryParse(str, out rv))
                    return rv;
                return Convert.ToInt64(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>
            /// <param name="defValue">缺省值</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;
                {
                    var IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(.w*)?$");
                    if (IsFloat)
                        float.TryParse(strValue, out intValue);
                }
                return intValue;
            }
    
    
            /// <summary>
            /// string型转换为Decimal型
            /// </summary>
            /// <param name="strValue">要转换的字符串</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>转换后的Decimal类型结果</returns>
            public static decimal StrToDecimal(string strValue, decimal defValue)
            {
                if ((strValue == null) || (strValue.Length > 10))
                    return defValue;
    
                decimal intValue = defValue;
                if (strValue != null)
                {
                    bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(.w*)?$");
                    if (IsFloat)
                        decimal.TryParse(strValue, out intValue);
                }
                return intValue;
            }
        }
    }

     

  • 相关阅读:
    PAT 1025. 反转链表 (25)
    PAT 1024. 科学计数法 (20)
    PAT 1076. Forwards on Weibo (30)
    C++——cout输出小数点后指定位数
    PTA 06-图3 六度空间 (30分)
    PTA 06-图2 Saving James Bond
    PTA
    浙大PTA
    浙大PTA
    随机密码生成
  • 原文地址:https://www.cnblogs.com/zhaoyl9/p/11104036.html
Copyright © 2011-2022 走看看