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

  • 相关阅读:
    nginx 配置下载text等文件
    linux 挂载硬盘
    linux 常用命令
    linux 常用目录的作用
    linux 增加新用户无法使用sudo命令解决办法
    linux 安装ifconfig
    linux 更换yum源
    eclipse 环境安装
    ORACLE 迁移MYSQL 随笔
    微信跳转显示空白页
  • 原文地址:https://www.cnblogs.com/johnnyzen/p/7991076.html
Copyright © 2011-2022 走看看