如何识别数字是否为整数?

时间:2014-11-29 17:08:57

标签: javascript

这是一个处理javascript和var类型标识的脚本。你在下面看到的是我正在做的一个小项目。它应该计算二次方程的x1和x2值。我遇到的问题是,我无法让程序识别结果是否为INTEGER格式,否则

这是我的代码:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="matrix_style.css">
<title>JS Calculator</title>

<script language="javascript" type="text/javascript">
    function result(){
    //first the values from table one are stored in the bellow variables:
    a=Number(document.table_1.number1.value);
    b=Number(document.table_1.number2.value);
    c=Number(document.table_1.number3.value);
    // then they are proccessed and stored in the ans1_1 and ans_2 variables:
    ans_1=-b + Math.sqrt(Math.pow(b, 2) - (4*a*c));
    ans_2=-b - Math.sqrt(Math.pow(b, 2) - (4*a*c));
    ans_3= "no solution"
    // here is my problem, I can't seem to be able to determine if the variables are integers meaning ... -2, -1, 0, 1, 2, ...
    // this part is suppose to dispaly the results only if the results are integers and else display "no solution"
    if(return ans_1%1===0 && return ans_2%===0 )
    {
        document.calculator.total_1.value=ans_1;
        document.calculator.total_2.value=ans_2;
    }
    else
    {
        document.calculator.total_3.value=ans_3;
    }

    }
</script>

</head>
  <body>


     <!--First the user inserts the a b and c values into the table bellow-->


    <table class="table_1">
     <form name="table_1" >
       <tr>
           <th>Insert your equation:</th>
           <th></th> 
           <th></th> 


        </tr>
        <tr>  
            <td><span id="A" contenteditable><input type="text" name="number1"  ><br></span></td>
            <td><span id="B" contenteditable><input type="text" name="number2"  ><br></span></td>
            <td><span id="C" contenteditable><input type="text" name="number3"  ><br></span></td>

        </tr>


    </form>
   </table>

     <!--The results are suppose to display in table bellow: -->

    <table class="table_result">
     <form name="calculator">
        <tr>

           <th>Result:</th>
            <th></th>     
        </tr>   
        <tr>  
            <td><span id="A_ans" contenteditable><input type="text" name="total_1"  ><br></span></td>
            <td><span id="B_ans" contenteditable><input type="text" name="total_2"  ><br></span></td>   
        </tr>   
        <tr>
            <td><span id="C_ans" contenteditable><input type="text" name="total_3" ><br></span></td>   
        </tr>    
            <input type="button" value="result" id="submit" onclick="javascript:result();">
    </form>
   </table>
  </body>
</html>

3 个答案:

答案 0 :(得分:4)

if(return ans_1%1===0 && return ans_2%===0 )是语法错误,应该是

if(ans_1%1===0 && ans_2%1===0 )

检查ans_2缺少模数操作数。

此外,您还要将减法传递给Math.pow(),这可能是否定的(导致NaN)。

答案 1 :(得分:0)

Javascript中没有“整数”,即整数,只有Numbers

检查它们是否为整数的最简单方法是round它们并与原始值进行比较:

if(Math.round(ans_1) === ans_1 && Math.round(ans_2) === ans_2)
{
    document.calculator.total_1.value = ans_1;
    document.calculator.total_2.value = ans_2;
}
else
{
    document.calculator.total_3.value = ans_3;
}

答案 2 :(得分:0)

  

除以1时检查余数:

function isInt(n) {
   return n % 1 === 0;
}
  

如果你不知道参数是一个数字 -

function isInt(n){
        return Number(n)===n && n%1===0;
}
  

参考此链接

     

is_int() =&gt;检查变量类型是否为Integer以及是否为Integer   内容是整数

     

is_float() =&gt;检查变量类型是否为Float及其是否为   内容是Float

相关问题