如何防止JavaScript计算器中的多个小数

时间:2018-11-04 08:03:07

标签: javascript function return-value calculator document

我正在制作一个JavaScript计算器,但小数位数有问题。我只需要输出一次即可。我将显示一个带点的示例(我需要0.55,代码可以设为0 .... 5)。此代码输出“ ”。在单击某个数字后,您仍然可以单击符号“。”它将在txtbox中写入许多点。这只是我所需代码的一部分

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
<style>
    button {
        user-select: none; 
        font-size:23px; 
        width:80px ; 
        height: 70px; 
        font-family:bold;
        border-radius: 50%;
        border-color: gray;
    }
    .ope{
        background: rgb(96, 250, 199);
    }
    .num:focus {
        background: lime;
    }

</style>
</head>

<body>
<input id="txtBox" type="text" style="text-align: right;font-size:60px;width: 325px;height: 125px; font-family: bold;" />
<br />
<button onclick="Click(event)" class="ope">C</button>
<button onclick="Click(event)" class="ope">delete</button>
<button onclick="Click(event)" class="ope">+/-</button>
<button onclick="Click(event)" class="ope">+</button>

<br />

<button onclick="Click(event)" class="num">1</button>
<button onclick="Click(event)" class="num">2</button>
<button onclick="Click(event)" class="num">3</button>
<button onclick="Click(event)" class="ope">-</button>

<br />

<button onclick="Click(event)" class="num">4</button>
<button onclick="Click(event)" class="num">5</button>
<button onclick="Click(event)" class="num">6</button>
<button onclick="Click(event)" class="ope">*</button>

<br />

<button onclick="Click(event)" class="num">7</button>
<button onclick="Click(event)" class="num">8</button>
<button onclick="Click(event)" class="num">9</button>
<button onclick="Click(event)" class="ope">/</button>

<br />

<button onclick="Click(event)" class="num">0</button>
<button onclick="Click(event)" id="ope">.</button>
<button onclick="Click(event)" id="equalto" style="background: rgb(206, 37, 206);user-select: none;font-size:25px; width:160px ; height: 70px; font-family:bold;">=</button>

<script>
    var nums = document.getElementById("num")
    var operator = document.getElementById("ope")
    var textbox = document.getElementById("txtBox")
    var counter = 0

    function Click(e) {
        var but = e.target;
        var maxamount = 9;
        var dot = 1
        if (but.innerText != "=" && but.innerText != "delete") {
            textbox.value += but.innerText
            if (but.innerText == "C") {
                textbox.value = "0"
            }
            else if (textbox.value[0] == "." || textbox.value[0] == "0" || textbox.value[0] == "+" || textbox.value[0] == "-" || textbox.value[0] == "/" || textbox.value[0] == "*") {
                textbox.value = ""
            }
            else if (textbox.value.length > maxamount) {
                textbox.value = textbox.value.substring(0, maxamount)
            }
            else if (textbox.value.includes == "+/-") {
                textbox.value*= -1
            }
            if (!textbox.value.includes('.') && textbox.value != "") {
                textbox.value+="."
            }
            else{

            }
        }

        else {
            textbox.value = eval(textbox.value)
        }
        if (but.innerText == "delete") {
            textbox.value = textbox.value.substring(0, textbox.value.length - 1);
        }
        if (textbox.value == "NaN" || textbox.value == "undefine") {
            textbox.value = ""
        }

    }
</script>
</body>

</html>

2 个答案:

答案 0 :(得分:2)

  Please try below Click function,

      <script>
        var nums = document.getElementById("num");
        var operator = document.getElementById("ope");
        var textbox = document.getElementById("txtBox");
        var counter = 0
        var dotReset=true;
        function Click(e) {
            var but = e.target;
            var maxamount = 9;
            var dot = 1;

            if (but.innerText != "=" && but.innerText != "delete") {
                if (but.innerText != ".") {
                    textbox.value += but.innerText;
                    if (but.innerText == "+" || but.innerText == "-" || but.innerText == "*" || but.innerText == "/"){
                        dotReset=true;
                    }
                }

                if (but.innerText == "C") {
                    textbox.value = "0";
                    dotReset=true;
                }
                else if (textbox.value[0] == "." || textbox.value[0] == "0" || textbox.value[0] == "+" || textbox.value[0] == "-" || textbox.value[0] == "/" || textbox.value[0] == "*") {
                    textbox.value = "";
                    dotReset=true;
                }
                else if (textbox.value.length > maxamount) {
                    textbox.value = textbox.value.substring(0, maxamount);
                }
                else if (textbox.value.includes == "+/-") {
                    textbox.value*= -1;
                }
                if (but.innerText == ".") {

                    if(dotReset){
                        if (textbox.value != "") {
                                textbox.value+="."
                                dotReset=false;
                            }
                    }

                    }
            }
            else {
                textbox.value = eval(textbox.value)
            }
            if (but.innerText == "delete") {
                textbox.value = textbox.value.substring(0, textbox.value.length - 1);
            }
            if (textbox.value == "NaN" || textbox.value == "undefine") {
                textbox.value = ""
            }

        }
    </script>

答案 1 :(得分:1)

我认为您的问题出在条件上(您的代码将 dot 按钮视为其他按钮,因此每次都会输出“。” )。您应该为 dot 按钮添加一个条件。您可以像下面这样重写代码:

var textbox = document.getElementById("txtBox")

function Click(e) {
  var but = e.target;
  if (but.innerText != "=" && but.innerText != "delete") {
     if (but.innerText == ".") {
        if (!textbox.value.includes('.') && textbox.value != "") {
           textbox.value += "."
        }
     }
     else{
        textbox.value += but.innerText
     }
  }
}

希望这会有所帮助。