简体   繁体   English

计算器:如何让它接受小数和百分比?

[英]Calculator: How to make it accept decimal numbers and percentage?

below I will put my html and js codes so that it is possible to better understand my doubt.下面我将把我的 html 和 js 代码放在一起,以便更好地理解我的疑问。 Right now, I'm trying to make it work with decimals like 3.5 + 3, and give the correct result: 6.5.现在,我正试图让它与 3.5 + 3 这样的小数一起工作,并给出正确的结果:6.5。 At the moment it rounds down and gives 6. I've already prepared the code to accept the comma, with the code part below, but I still don't understand how to fix it:目前它向下舍入并给出 6。我已经准备好接受逗号的代码,下面是代码部分,但我仍然不明白如何修复它:

if (button.classList.contains("number")) {
      if (buttonValue === ",") {
          if (currentValue.indexOf(",") === -1) {
              currentValue += buttonValue;
          }
      } else {
          currentValue += buttonValue; //concatena
      }
      currentOperation.textContent = currentValue; //actualiza el currentOperation para mostrar el valor actual
    }

Another thing is the percentage that is not working either...另一件事是百分比也不起作用......

            case "%":
                result = 100 * parseFloat(previousValue) / parseFloat(currentValue);
                break;

Below I leave the html and js codes.下面我留下html和js代码。 Thank you to all who are willing to help me find a solution!感谢所有愿意帮助我找到解决方案的人!

 const buttons = document.querySelectorAll(".buttons-container button"); //obtiene todos los botones const currentOperation = document.querySelector(".current-operation"); //mostrar la operación actual y el resultado del cálculo. currentOperation.textContent = "0"; buttons.forEach(button => button.addEventListener("click", handleButtonClick)); //un detector de eventos de clic, llama a la handleButtonClick función a cada clic let currentValue = ""; //valor actual ingresado let previousValue = ""; //valor anterior ingresado let operation = ""; //operación actual function handleButtonClick(event) { const button = event.target; //event.target = propiedad para obtener el botón en el que se hizo clic const buttonValue = button.textContent; //textContent = propiedad para obtener el texto del botón //botones numéricos if (button.classList.contains("number")) { if (buttonValue === ",") { if (currentValue.indexOf(",") === -1) { currentValue += buttonValue; } } else { currentValue += buttonValue; //concatena } currentOperation.textContent = currentValue; //actualiza el currentOperation para mostrar el valor actual } //botones del operador if (buttonValue === "+" || buttonValue === "-" || buttonValue === "*" || buttonValue === "/" || buttonValue === "%") { operation = buttonValue; currentOperation.textContent = operation; previousValue = currentValue; //actualiza previousValue currentValue = ""; //borra la currentValue } //botón equal if (button.classList.contains("equal-btn")) { let result; //Realizar la operación switch (operation) { case "+": result = parseFloat(previousValue) + parseFloat(currentValue); break; case "-": result = parseFloat(previousValue) - parseFloat(currentValue); break; case "*": result = parseFloat(previousValue) * parseFloat(currentValue); break; case "/": result = parseFloat(previousValue) / parseFloat(currentValue); break; case "%": result = 100 * parseFloat(previousValue) / parseFloat(currentValue); break; } //Mostrar el resultado y borrar los valores currentOperation.textContent = result; previousValue = ""; currentValue = ""; } //botón AC if (button.classList.contains("AC-btn")) { previousValue = ""; currentValue = ""; currentOperation.textContent = "0"; } }
 <div class="calc"> <div class="operations"> <,--display--> <div class="name">Calculadora</div> <div class="current-operation"></div> </div> <div class="buttons-container"> <button class="AC-btn">AC</button> <button>%</button> <button>/</button> <button class="number">7</button> <button class="number">8</button> <button class="number">9</button> <button>*</button> <button class="number">4</button> <button class="number">5</button> <button class="number">6</button> <button>-</button> <button class="number">1</button> <button class="number">2</button> <button class="number">3</button> <button>+</button> <button class="number">0</button> <button class="number">,</button> <button class="equal-btn">=</button> </div> </div>

parseFloat doesn't work with the comma. parseFloat 不适用于逗号。 Try replacing it with a dot instead:尝试用点替换它:

parseFloat(value.replace(",", "."));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM