将十进制添加到计算器

时间:2014-08-04 06:35:45

标签: javascript decimal add calculator

我使用javascript制作了一个计算器,它工作正常,但它有一些问题。

  1. 当我想计算数字的根本时,当我点击激进按钮时,它不起作用,但当我双击它时,它可以工作!
  2. 我不知道如何在我的计算器中添加小数,请帮帮我
  3. 这是javascript代码:

    <script type="text/javascript">
            var valid=false ,operand='', numCounter=1, firstNum=null, secondNum=null;
            function remov(){
                document.getElementById('result').value = null;
                secondNum = firstNum = null;
                numCounter=1;
            }
            function show(id){
                var val = document.getElementById(id).value;
                if(id=='+' || id=='-' || id=='*' || id=='/' || id=='^' || id=='√')
                {
                    valid=true;
                    if(numCounter > 1){
                        calc();
                    }
                    firstNum=Number(document.getElementById('result').value);
                    numCounter++;
                    operand=id;
                }
                else if(id == 'equal')
                {
                    calc();
                    numCounter=1;
                }
                else
                {
                    if(valid){
                    document.getElementById('result').value = null;
                    valid=false;
                    }
                    document.getElementById('result').value += val;
                }
            }
            function calc(){
                if(numCounter > 1){
                    secondNum=Number(document.getElementById('result').value);
                }else{
                    firstNum=Number(document.getElementById('result').value);
                }
                switch(operand)
                {
                    case '+':
                    document.getElementById('result').value = firstNum+secondNum;
                    break;
                    case '-':
                    document.getElementById('result').value = firstNum-secondNum;
                    break;
                    case '/':
                    if(firstNum != 0)
                    document.getElementById('result').value = firstNum/secondNum;
                    else
                        document.getElementById('result').value = "Error";
                    break;
                    case '*':
                    document.getElementById('result').value = firstNum*secondNum;
                    break;
                    case '^':
                    document.getElementById('result').value = Math.pow(firstNum,secondNum);
                    break;
                    case '√':
                    document.getElementById('result').value = Math.sqrt(firstNum);
                    break;
                }
            }
        </script>
    

    这是HTML

    <body oncontextmenu="return false">
        <div id="cal">
            <input id="result" type="text" readonly /><br/>
            <button value="1" id="1" onclick="show(1)">1</button>
            <button value="2" id="2" onclick="show(2)">2</button>
            <button value="3" id="3" onclick="show(3)">3</button>
            <button value="+" id="+" onclick="show('+')">+</button><br/>
            <button value="4" id="4" onclick="show(4)">4</button>
            <button value="5" id="5" onclick="show(5)">5</button>
            <button value="6" id="6" onclick="show(6)">6</button>
            <button value="-" id="-" onclick="show('-')">-</button><br/>
            <button value="7" id="7" onclick="show(7)">7</button>
            <button value="8" id="8" onclick="show(8)">8</button>
            <button value="9" id="9" onclick="show(9)">9</button>
            <button value="*" id="*" onclick="show('*')">*</button><br/>
            <button value="." id="." onclick="show('.')">.</button>
            <button value="0" id="0" onclick="show(0)">0</button>
            <button value="√" id="√" onclick="show('√')">√</button>
            <button value="/" id="/" onclick="show('/')">/</button><br/>
            <button onclick="remov()">C</button>
            <button value="^" id="^" onclick="show('^')">^</button>
            <button id="equal" value="=" onclick="show('equal')">=</button>
        </div>
    </body>
    

1 个答案:

答案 0 :(得分:1)

对于小数,您可以使用toFixed - http://www.w3schools.com/jsref/jsref_tofixed.asp

根据其他问题,不知道。当您只点击一次时,它是否适用于其他按钮?

相关问题