zoukankan      html  css  js  c++  java
  • js 判断 -数字- 的方法

    判断数字的方法

    var a = typeof 5,
        b = typeof Infinity,
        c = typeof NaN,
        d = typeof 0,
        e = 0;
    console.log(a, isNaN(5))    //number false
    console.log(b, isNaN(Infinity))     //number false
    console.log(c, isNaN(NaN))          //number true
    console.log(d, isNaN(0))            //number false
    
    
    // Infinity 这。。也是true,有点麻烦
    console.log(Boolean(Infinity)); //true
    
    
    function checkNum(item) {
        // var result = "start";
        // if (!isNaN(item)) {
        //     result = "not NaN";
        //     if (isFinite(item)) {
        //         if (item || item === 0) {
        //             result = item;
        //         }
        //     }else{
        //         result = "not finite";
        //     }
        // } else {
        //     result = "is NaN";
        // }
        // console.log("result: " + result);
    
        // 上面例子可见,使用 isNan 和 isFinite 可以排除掉非数字的数据
        // 最好再加上 parseInt/parseFloat
        // 字符串的话也可以用正则 d
    }
    
    checkNum(5)         // 5
    checkNum(Infinity)  //not finite
    checkNum(NaN)       //is NaN
    checkNum(0)         //0
    
    function checkNum(item) {
        var num = parseInt(item, 10);            //第二个参数指定用10进制,ie8默认会用8进制。。。
        if(!isNaN(num) && isFinite(num)) {
            if (item || item === 0) {
                console.log(item);
                return;
            }
        }
        console.log("not a number");
    }
    
    checkNum(5)         // 5
    checkNum(Infinity)  //not a number
    checkNum(NaN)       //not a number
    checkNum(0)         //0
    
    
  • 相关阅读:
    C# WM_NCMOUSELEAVE 消息触发
    C#常用集合的使用(转载)
    关于直播,所有的技术细节都在这里了(转载)
    C# Winform 窗体美化
    正则表达式符号全解析
    C#中List<T>转DataTable
    C#中的Queue集合
    C#中Stack集合
    智能信息处理
    Mysql
  • 原文地址:https://www.cnblogs.com/xfh0192/p/7653460.html
Copyright © 2011-2022 走看看