网站首页

人工智能P2P分享Wind搜索发布信息网站地图标签大全

当前位置:诺佳网 > 软件工程 > 后端开发 > .Net >

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

时间:2026-02-23 15:01

人气:

作者:admin

标签:

导读:正在手搓 NumType 类型检查器, 它可以通过输入文本信息来判断这个文本它可以转换成什么类型, 挺复杂的, 在此过程了解了一些我没有见过的变量类型, 然后这两天又是为着边境检查, 选取...

正在手搓 NumType 类型检查器, 它可以通过输入文本信息来判断这个文本它可以转换成什么类型, 挺复杂的, 在此过程了解了一些我没有见过的变量类型, 然后这两天又是为着边境检查, 选取范围感到苦恼, 不过现在已经把基础代码给写好了, 勉强能用, 暂时没有出现什么大Bug (但愿吧).

/// <summary>
/// 判断数字类型
/// </summary>
/// <param name="input">输入文本</param>
/// <returns>返回类型: null, string, int, long, float, double, decimal, biginteger</returns>
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 < input.Length; i++ ) //遍历文本
    {
        char t = input[i];
        if (i == 0 && (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[i - 1] == 'E' || input[i - 1] == 'e')
            {
                yes = true;
            }
        }
        else if (t == '.' && 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[0] == '-')
        {
            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 && (input[input.Length - 1] == '-' || input[input.Length - 1] == '+')) //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 < 0) //加小数点
            {
                int newpot = E + potindex;
                int length = input.Length;
                if (length > newpot) {
                    for (int t = 0; t < newpot * -1 + 1; t++)
                    {
                        input = "0" + input;
                    }
                }
            }
            else //减小数点
            {
                int newpot = E + potindex;
                int length = input.Length;
                if (newpot > length) {
                    for (int t = 0; t < newpot - length; t++)
                    {
                        input = input + "0";
                    }
                }
                //好像没必要再把小数点填回去, 后面只比对有多少位有效数字
            }
            //ConsoleLog(input);
        }

        //if (input.Substring(input.IndexOf("."), endindex).Length > 16 ||
        //    input.Substring(0, input.IndexOf(".")).Length > 16) //高精度浮点
        input = input.Replace(".","");
        //继续比对
        if (input.Length > 17) //高精度浮点
        {
            return "decimal";
        }
        else
        {
            if (double1 > float.MaxValue) //双精度浮点
            {
                return "double";
            }
            //else if (input.Substring(input.IndexOf("."), endindex).Length > 7 ||
            //    input.Substring(0, input.IndexOf(".")).Length > 7) //超过单精度范围还是双精度浮点
            else if (input.Length > 8)
            {
                return "double";
            }
            else //单精度浮点
            {
                return "float";
            }
        }
    }
}
温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信