我正在制作一个 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>
最佳答案
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>
关于javascript - 如何防止javascript计算器中出现多个小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53138851/