Example #1
0
        /// <summary>
        /// 将对象转换成int类型,转换失败将返回defValue
        /// </summary>
        /// <param name="Expression"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static int StrToInt(object Expression, int defValue)
        {
            if (Expression == null)
            {
                return(defValue);
            }
            string input = Expression.ToString().Trim();

            //是小数的情况,先转成decimal
            if (input.IndexOf(".") != -1)
            {
                input = TryParse.StrToDecimal(Expression).ToString("f0");
            }
            if (((input.Length <= 0) || (input.Length > 11)) || !Regex.IsMatch(input, @"^[-]?\d*[.]?\d*$"))
            {
                return(defValue);
            }

            if (((input.Length >= 10 && (input[0] != '1' && input[0] != '2')) || (input.Length >= 10 && input[0] == '-' && input[1] != '1' && input[1] != '2')))
            {
                return(defValue);
            }
            try
            {
                return(Convert.ToInt32(input));
            }
            catch
            {
                return(defValue);
            }
        }
Example #2
0
        /// <summary>
        /// 取两数相减结果
        /// </summary>
        /// <param name="val1"></param>
        /// <param name="val2"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static decimal NumberMinus(object val1, object val2, decimal defValue)
        {
            decimal value = TryParse.StrToDecimal(val1) - TryParse.StrToDecimal(val2);

            if (value < 0m)
            {
                return(defValue);
            }
            return(value);
        }
Example #3
0
        /// <summary>
        /// 取两数相除结果,除数为0返回0
        /// </summary>
        /// <param name="val1"></param>
        /// <param name="val2"></param>
        /// <returns></returns>
        public static decimal NumberDivide(object val1, object val2)
        {
            if (TryParse.StrToDecimal(val2) == 0)
            {
                return(0);
            }
            decimal value = TryParse.StrToDecimal(val1) / TryParse.StrToDecimal(val2);

            return(value);
        }