zoukankan      html  css  js  c++  java
  • 在javascript中检查一个值是否为integer

    integer 类型在javascript中很奇怪。ECMAScript技术规格说明书中,它是以概念的形式存在。number类型包括浮点型(floating )和整形(integer )不包括小数(详情参考 “Integers in JavaScript” in “Speaking JavaScript”)。在这篇博客中Dr. Axel Rauschmayer  大师将解释如何检查一个值是否为integer。 

    ECMAScript 5

    有很多方式可以检查一个值是否为integer. 在此, 你可以休息一下并且试着写一下你自己的解决方案。例如: 定义一个 function isInteger(x),如果它是(integer )让它返回 true  或者返回 false。

    让我们看一下大师的小例子.

    使用整除进行检查(Checking via the remainder operator)

    首先我们可以使用 remainder operator (%) 去运算 number类型是不是一个 integer。number 类型整除 1 的结果为 0,那么他就是一个整型.

     function isInteger(x) {
            return x % 1 === 0;
        }

    大师喜欢这种解决办法, 因为它相当于自我描述.  运算的结果如下:

        > isInteger(17)
        true
        > isInteger(17.13)
        false

    使用整除符号的时候你必须小心, 因为第一个运算对象确定了操作结果的正负性: 如果它是正数, 那么结果就是正数。相反的,如果他是负数,那么记过就是负数.

        > 3.5 % 1
        0.5
        > -3.5 % 1
        -0.5

    我们也可以检查传入的值是不是 zero(0,这里 zero(0)不是讨论的重点. 但是会有一个问题出现: 这个function 会当传入的值为non-number时 return true   因为 整除符号(%)会强制将传入的值转换为 numbers:

        > isInteger('')
        true
        > isInteger('33')
        true
        > isInteger(false)
        true
        > isInteger(true)
        true

    所以这里我们在加上一个类型的判断(typeof):

       function isInteger(x) {
            return (typeof x === 'number') && (x % 1 === 0);
        }

    使用Math.round()方法进行检查(Checking via Math.round())

    如果一个number类型调用round()函数后很接近Integer类型,那么他就是Integer类型. 方法来自于 JavaScript的 Math.round():

        function isInteger(x) {
            return Math.round(x) === x;
        }

    运算结果如下:

        > isInteger(17)
        true
        > isInteger(17.13)
        false

    这样也控制了 non-numbers 的正确性, 因为Math.round() 总是返回 numbers。  如果===两边的类型一致结果返回 true.

     > isInteger('')
        false

    如果你想让你的代码计算更为清楚,你可以添加类型的检查(如我们前面所做的 typeof). 此外, Math.floor() 和Math.ceil() 也可以像Math.round()一样来判断是否为Integer.

    使用位运算付进行检查(Checking via bitwise operators)

    位操作符提供了一种   number 转换为 integer的方法:

       function isInteger(x) {
            return (x | 0) === x;
        }

    该解决方案(与基于运算符的其他解决方案有一个缺点:它不能处理超过32数字.

        > isInteger(Math.pow(2, 32))
        false

    使用parseInt()进行检查(Checking via parseInt())

    parseInt() 也提供了将 numbers 转换为 integers 的方法,使用起来类似于 Math.round(). 我们来可以下parseInt()是不是一个好的解决办法.

     function isInteger(x) {
            return parseInt(x, 10) === x;
        }

     拿Math.round() 的解决方案来说,  对 non-numbers 的处理很好,但是他不能检测所有的numbers类型是不是integers类型:

      > isInteger(1000000000000000000000)
        false

    Why? parseint()强制它的第一个参数解析为字符串在进行数字运算之前。这不是一个很好的选择,将数字转换成整数.

        > parseInt(1000000000000000000000, 10)
        1
        > String(1000000000000000000000)
        '1e+21'

    以上, 就是parseInt() 在解析 '1e+21'时停止解析,在解析到e的时候停止数字运算了, 这就是为什么返回1.

    Other solutions

    大神他相信会有更多的解决方案在 Twitter中, 感兴趣的朋友check them out.

    ECMAScript 6

    补充 Math.round() et al., ECMAScript 6 提供了额外的 numbers 转换为 integers的方式: Math.trunc(). 这个函数删除了number的小数部分:

        > Math.trunc(4.1)
        4
        > Math.trunc(4.9)
        4
        > Math.trunc(-4.1)
        -4
        > Math.trunc(-4.9)
        -4
    

    不久的将来, ECMAScript 6 将添加对 integers类型的琐碎校验, 因为添加了 Number.isInteger()方法.

    Further reading

    • Converting to Integer” (in “Speaking JavaScript”) covers the most common ways of converting numbers to integers.
    • Safe Integers” (in “Speaking JavaScript”) explains what range of integers can be safely used in JavaScript and what “safely used” means.

    备注:此文章源自 Dr. Axel Rauschmayer ,一切版权都为大师所有。

    详情见大师blog:http://www.2ality.com/2014/05/is-integer.html

    Speaking JavaScript也是大师不错的一本书,链接地址为:http://speakingjs.com/es5/index.html#pt03

  • 相关阅读:
    mysql修改加密方式
    信息安全学习路线
    DNS域传送漏洞
    CSRF漏洞
    反序列化漏洞
    计算机通讯基础
    gorm gen使用
    golang makefile使用
    linux命令行录制
    go代码自动生成注释
  • 原文地址:https://www.cnblogs.com/YcYYcY/p/3759184.html
Copyright © 2011-2022 走看看