字符串转换为整数

  class Program
    {
        static void Main(string[] args)
        {

  int result = StrConverter.Convert("123.45");
            Console.WriteLine(result);

            try
            {
                double t1 = int.MaxValue;
                t1 = t1 + 1;
                int max = StrConverter.Convert(t1.ToString());
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);
            }

            Console.Read();

}}

 

 /// <summary>
    /// 转换类
    /// </summary>
   public  class StrConverter
    {

        /// <summary>
        /// 将形如 123.45 这样的字符串,转化为 123 这样的整型数
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
      public  static Int32 Convert(string src)
        {

            if (string.IsNullOrEmpty(src))
            {
                throw new Exception("不可为空");
            }

            bool positive = true;
            Int32 result = 0;
            double tempResult = 0;
            int start = 0;

            while (start <= src.Length - 1 && src[start] == ' ')
            {

                if (start == Int16.MaxValue)
                {

                    throw new Exception("空格太多" + start);


                }
                start++;

            }


            if (start <= src.Length - 1 && src[start] == '-')
            {
                positive = false;
                start++;
            }


            for (int i = start; i < src.Length; i++)
            {
                int charInt = FromChar2Int(src[i]);
                if (IsNumber(src[i]))
                {

                    tempResult = tempResult * 10 + charInt;
                    result = result * 10 + charInt;

                }
                else if (src[i] == '.')
                {
                    break;
                }
                else
                {
                    throw new Exception("非法字符.位置" + i + " " + src[i]);
                }

                if (positive)
                {
                    if (tempResult > ((double)Int32.MaxValue - 1))
                    {
                        throw new Exception("超出整形范围,太大了");
                    }
                }
                else
                {
                    if (tempResult > ((double)Int32.MaxValue + 1))
                    {
                        throw new Exception("超出整形范围,太小了");
                    }
                }


            }

            if (positive)
            {
                return result;
            }
            else
            {
                return -1 * result;
            }


        }

        static bool IsNumber(char c)
        {
            int code = (int)c - 48;
            if (code > 9 || code < 0)
            {
                return false;
            }
            return true;
        }

        static Int32 FromChar2Int(char c)
        {
            int code = (int)c - (int)'0';
            return code;
        }

   
    }

 

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值