十里天国 發表於 2026-2-23 15:01:00

手搓一个数字类的变量的类型检查器

<p>正在手搓 NumType 类型检查器, 它可以通过输入文本信息来判断这个文本它可以转换成什么类型, 挺复杂的, 在此过程了解了一些我没有见过的变量类型, 然后这两天又是为着边境检查, 选取范围感到苦恼, 不过现在已经把基础代码给写好了, 勉强能用, 暂时没有出现什么大Bug (但愿吧).</p>
<pre><code class="language-Csharp">/// &lt;summary&gt;
/// 判断数字类型
/// &lt;/summary&gt;
/// &lt;param name="input"&gt;输入文本&lt;/param&gt;
/// &lt;returns&gt;返回类型: null, string, int, long, float, double, decimal, biginteger&lt;/returns&gt;
public static string NumType(string input)
{
    //忽略 Null
    if (input == null)
    {
      return "null";
    }
    else if(input.Length == 0)
    {
      return "string";
    }

    string type = "string";
    char[] nums = {'1','2','3','4','5','6','7','8','9','0' };
    bool kexuejishufa = false; //是不是科学计数发
    for(int i = 0; i &lt; input.Length; i++ ) //遍历文本
    {
      char t = input;
      if (i == 0 &amp;&amp; (t == '-' || t == '+')) //是不是正负数
      {
            continue;
      }
      bool yes = false;
      foreach(char tt in nums) //诶个看看单个字是不是数字
      {
            if (tt == t)
            {
                yes = true;
                break;
            }
      }
      if (t == 'e' || t == 'E') //科学计数发
      {
            if (kexuejishufa == true) //不可能有两个E
            {
                return "string";
            }
            else
            {
                yes = true;
                type = "float";
                kexuejishufa = true;
            }
      }
      else if(t == '-' || t == '+') //正负数的前面是不是 'E'
      {
            if (input == 'E' || input == 'e')
            {
                yes = true;
            }
      }
      else if (t == '.' &amp;&amp; type != "float") //小数点
      {
            yes = true;
            type = "float";
      }
      if (yes == false) //都不是那只能是文本
      {
            return "string";
      }
    }
    //都跑到这里来的,说明是数字
    if (type != "float")
    {
      type = "int";
    }
    //然后判断大小
    if(type == "int") //这是整数
    {
      int int1 = 0;
      long long1 = 0;
      decimal decimal1 = 0;
      if (int.TryParse(input,out int1) == true) //32位整數
      {
            return "int";
      }
      else if (long.TryParse(input,out long1) == true) //64位整數
      {
            return "long";
      }
      else if (decimal.TryParse(input,out decimal1) == true) //128位數字
      {
            return "decimal";
      }
      else //超級無敵大
      {
            return "biginteger";
      }
    }
    else //这是浮点数
    {
      double double1 = 0;
      //去除符号
      if (input == '-')
      {
            input = input.Substring(1);
      }
      //科学计数法展开
      if (kexuejishufa == true)
      {
            int E = input.IndexOf("E", StringComparison.InvariantCultureIgnoreCase);
            //检查 E 旁边有没有数字
            if (E == 0 || E == input.Length - 1) // 1e
            {
                return "string";
            }
            else if(E == input.Length - 2 &amp;&amp; (input == '-' || input == '+')) //1e+
            {
                return "string";
            }
            //获取 E 后边的数
            E = int.Parse(input.Substring(E + 1, input.Length - 1 - E));
            input = input.Substring(0, input.IndexOf("E", StringComparison.InvariantCultureIgnoreCase));
            int potindex = input.IndexOf(".");
            input = input.Replace(".", "");
            if (E &lt; 0) //加小数点
            {
                int newpot = E + potindex;
                int length = input.Length;
                if (length &gt; newpot) {
                  for (int t = 0; t &lt; newpot * -1 + 1; t++)
                  {
                        input = "0" + input;
                  }
                }
            }
            else //减小数点
            {
                int newpot = E + potindex;
                int length = input.Length;
                if (newpot &gt; length) {
                  for (int t = 0; t &lt; newpot - length; t++)
                  {
                        input = input + "0";
                  }
                }
                //好像没必要再把小数点填回去, 后面只比对有多少位有效数字
            }
            //ConsoleLog(input);
      }

      //if (input.Substring(input.IndexOf("."), endindex).Length &gt; 16 ||
      //    input.Substring(0, input.IndexOf(".")).Length &gt; 16) //高精度浮点
      input = input.Replace(".","");
      //继续比对
      if (input.Length &gt; 17) //高精度浮点
      {
            return "decimal";
      }
      else
      {
            if (double1 &gt; float.MaxValue) //双精度浮点
            {
                return "double";
            }
            //else if (input.Substring(input.IndexOf("."), endindex).Length &gt; 7 ||
            //    input.Substring(0, input.IndexOf(".")).Length &gt; 7) //超过单精度范围还是双精度浮点
            else if (input.Length &gt; 8)
            {
                return "double";
            }
            else //单精度浮点
            {
                return "float";
            }
      }
    }
}
</code></pre><br><br>
来源:https://www.cnblogs.com/yuhang0000/p/19631268
頁: [1]
查看完整版本: 手搓一个数字类的变量的类型检查器