• <legend id='tIXMx'><style id='tIXMx'><dir id='tIXMx'><q id='tIXMx'></q></dir></style></legend>
    • <bdo id='tIXMx'></bdo><ul id='tIXMx'></ul>

      <small id='tIXMx'></small><noframes id='tIXMx'>

      <i id='tIXMx'><tr id='tIXMx'><dt id='tIXMx'><q id='tIXMx'><span id='tIXMx'><b id='tIXMx'><form id='tIXMx'><ins id='tIXMx'></ins><ul id='tIXMx'></ul><sub id='tIXMx'></sub></form><legend id='tIXMx'></legend><bdo id='tIXMx'><pre id='tIXMx'><center id='tIXMx'></center></pre></bdo></b><th id='tIXMx'></th></span></q></dt></tr></i><div id='tIXMx'><tfoot id='tIXMx'></tfoot><dl id='tIXMx'><fieldset id='tIXMx'></fieldset></dl></div>

      <tfoot id='tIXMx'></tfoot>

        如何检查 JavaScript 数字是否是真实有效的数字?

        时间:2023-06-13
      1. <tfoot id='sW4X7'></tfoot>

          <i id='sW4X7'><tr id='sW4X7'><dt id='sW4X7'><q id='sW4X7'><span id='sW4X7'><b id='sW4X7'><form id='sW4X7'><ins id='sW4X7'></ins><ul id='sW4X7'></ul><sub id='sW4X7'></sub></form><legend id='sW4X7'></legend><bdo id='sW4X7'><pre id='sW4X7'><center id='sW4X7'></center></pre></bdo></b><th id='sW4X7'></th></span></q></dt></tr></i><div id='sW4X7'><tfoot id='sW4X7'></tfoot><dl id='sW4X7'><fieldset id='sW4X7'></fieldset></dl></div>

                <tbody id='sW4X7'></tbody>

              <small id='sW4X7'></small><noframes id='sW4X7'>

              • <bdo id='sW4X7'></bdo><ul id='sW4X7'></ul>
                <legend id='sW4X7'><style id='sW4X7'><dir id='sW4X7'><q id='sW4X7'></q></dir></style></legend>

                • 本文介绍了如何检查 JavaScript 数字是否是真实有效的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的代码是:

                  function isNumber(n){
                  return typeof n == 'number' && !isNaN(n);
                  }
                  
                  window.onload=function(){
                  var a=0,b=1,c=2.2,d=-3,e=-4.4,f=10/3;
                  var shouldBeTrue=[a,b,c,d,e,f];
                  
                  var aa="0",bb="1",cc="2.2",dd="-3",ee="-4.4",ff="10/3";
                  var shouldBeFalse=[aa,bb,cc,dd,ee,ff];
                  
                  var aaa,bbb=true,ccc=false,ddd=document.getElementsByTagName('html');
                  var alsoTheseBeFalse=[aaa,bbb,ccc,ddd,""," ",,null,NaN];
                  
                  for(var i=0;i<shouldBeTrue.length;i++)
                      if(isNumber(shouldBeTrue[i]) != true) alert("x");
                  for(i=0;i<shouldBeFalse.length;i++)
                      if(isNumber(shouldBeFalse[i]) != false) alert("x");
                  for(i=0;i<alsoTheseBeFalse.length;i++)
                      if(isNumber(alsoTheseBeFalse[i]) != false) alert("x");
                  }
                  

                  我还应该检查什么以确保我的功能在所有方面都 101% 完美?(另外,如果你知道更好的功能请告诉我)

                  What else should I check against to ensure my function is 101% perfect in all ways? (also, if you know a better function please tell me)

                  推荐答案

                  如果要检查一个数是否为实数,还应该检查它是否是有限的:

                  If you want to check whether a number is a real number, you should also check whether it's finite:

                  function isNumber(n){
                      return typeof n == 'number' && !isNaN(n) && isFinite(n);
                   }
                  

                  另一种方法(解释如下):

                  Another method (explanation below):

                  function isNumber(n){
                      return typeof n == 'number' && !isNaN(n - n);
                  }
                  

                  更新:验证实数的两个表达式

                  由于 JavaScript 数字表示实数,相同数字的减法操作数应该产生零值 (additive身份).超出范围的数字应该(并且将会)无效,NaN.

                  Update: Two expressions to validate a real number

                  Since JavaScript numbers are representing real numbers, the substraction operand on the same number should produce the zero value (additive identity). Numbers out of range should (and will) be invalid, NaN.

                  1        - 1        = 0    // OK
                  Infinity - Infinity = NaN  // Expected
                  NaN      - NaN      = NaN  // Expected
                  NaN      - Infinity = NaN
                  

                  这篇关于如何检查 JavaScript 数字是否是真实有效的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:专门为页面上的所有数字设置字体 下一篇:如何防止在html中的数字字段中插入大于最大值的值

                  相关文章

                    1. <i id='pHPf0'><tr id='pHPf0'><dt id='pHPf0'><q id='pHPf0'><span id='pHPf0'><b id='pHPf0'><form id='pHPf0'><ins id='pHPf0'></ins><ul id='pHPf0'></ul><sub id='pHPf0'></sub></form><legend id='pHPf0'></legend><bdo id='pHPf0'><pre id='pHPf0'><center id='pHPf0'></center></pre></bdo></b><th id='pHPf0'></th></span></q></dt></tr></i><div id='pHPf0'><tfoot id='pHPf0'></tfoot><dl id='pHPf0'><fieldset id='pHPf0'></fieldset></dl></div>
                        <bdo id='pHPf0'></bdo><ul id='pHPf0'></ul>
                      <legend id='pHPf0'><style id='pHPf0'><dir id='pHPf0'><q id='pHPf0'></q></dir></style></legend>

                      <small id='pHPf0'></small><noframes id='pHPf0'>

                      <tfoot id='pHPf0'></tfoot>