zoukankan      html  css  js  c++  java
  • JavaScript之判断参数的数值的详细类型

    //判断是否为字符串
    //返回类型:
    //{baseType:typeof(arg),numberType:'int','float',-1}
    function numberType(arg){
        var baseType;
        var numberType;
        var regx_int =  /^[0-9]*[1-9][0-9]*$/;
        if(!isNaN(arg)){//JavaScript中,属于数值的有:数值型字符串和真实数值       
            baseType = typeof(arg);//输出:string or number
            if(regx_int.test(arg)){//整数
                numberType = "int";
            } else {
                numberType = "float";
            }
        } else {
            baseType = typeof(arg);
            numberType = -1;
        }
    
        return {
            "arg":arg,
            "baseType": baseType,//注意:typeof(stringNumber):string
            "numberType": numberType
        }
    }
    
    /*
        console.log(numberType("23"));  //{arg: "23", baseType: "string", numberType: "int"}
        console.log(numberType("23.0"));//{arg: "23.0", baseType: "string", numberType: "float"}
        console.log(numberType("23a")); //{arg: "23a", baseType: "string", numberType: -1}
        console.log(numberType(23));    //{arg: 23, baseType: "number", numberType: "int"}
        console.log(numberType(23.0));  //{arg: 23, baseType: "number", numberType: "int"}
     */

    参考文献:

      http://blog.csdn.net/xingfeng0501/article/details/6681912

  • 相关阅读:
    nyoj 17 单调递增最长子序列
    nyoj 18 The Triangle
    nyoj 712 探 寻 宝 藏
    nyoj 61传纸条(一)
    nyoj 269 VF
    nyoj 44 子串和
    nyoj 252 01串
    nyoj 42 一笔画问题
    nyoj 756 重建二叉树
    Table 样式设置
  • 原文地址:https://www.cnblogs.com/johnnyzen/p/7991076.html
Copyright © 2011-2022 走看看