My Float在此代码中不起作用

时间:2018-03-15 21:26:58

标签: javascript html

所以在这段代码中我有一个计算,但Float部分不工作,如果我把1.32 + 1.32然后它等于2,所以我想它是1.32 + 1.32 = 2.64 这可能在这段代码中

我在这里添加更多详细信息,因为它一直给我错误,所以很抱歉在这里输入了很多细节

calcBtn.addEventListener('click', function() {
 calc();
});



function calc() {
  var number1 = parseFloat(document.getElementById('number1').value);
  var number2 = parseFloat(document.getElementById('number2').value);
  var number1 = parseInt(document.getElementById('number1').value);
  var number2 = parseInt(document.getElementById('number2').value);
  var opera = document.getElementById('operaatorid').value;

if (opera === '+') {
 var result = number1 + number2;
 document.getElementById('tulemus').value = result;
 var calcString = number1 + opera + number2 + "=" + result;
 document.getElementById('lastCalc').innerHTML = calcString;
 previousCalc = calcString;
 document.getElementById('PreviousCalc').innerHTML += previousCalc + "<br>";

}
if (opera === '-') {
  var result = number1 - number2;
  document.getElementById('tulemus').value = result;
  var calcString = number1 + opera + number2 + "=" + result;
  document.getElementById('lastCalc').innerHTML = calcString;
  previousCalc = calcString;
  document.getElementById('PreviousCalc').innerHTML += previousCalc + "<br>";

}
if (opera === '/') {
  var result = number1 / number2;
  document.getElementById('tulemus').value = result;
  var calcString = number1 + opera + number2 + "=" + result;
  document.getElementById('lastCalc').innerHTML = calcString;
  previousCalc = calcString;
  document.getElementById('PreviousCalc').innerHTML += previousCalc + "<br>";

}
if (opera === '*') {
  var result = number1 * number2;
  document.getElementById('tulemus').value = result;
  var calcString = number1 + opera + number2 + "=" + result;
  document.getElementById('lastCalc').innerHTML = calcString;
  previousCalc = calcString;
  document.getElementById('PreviousCalc').innerHTML += previousCalc + <br>";

 }


}

2 个答案:

答案 0 :(得分:1)

因为这个

,你的浮动无法正常工作
var number1 = parseFloat(document.getElementById('number1').value);
...
var number1 = parseInt(document.getElementById('number1').value);
...

您首先选择float value in number1,然后同时存储int value in number1以前的float value will be replace by int值,因此您将2 2.64 <{1}} < / p>

答案 1 :(得分:0)

在设置基于number1的{​​{1}}和number2变量后,您重新声明它们使用parseFloat(),这会剥离浮点部分这个号码,这就是为什么你只得parseInt()

2

如果你坚持// This was right: var number1 = parseFloat(document.getElementById('number1').value); var number2 = parseFloat(document.getElementById('number2').value); // But, then you overwrote the variables with parseInt() based values: var number1 = parseInt(document.getElementById('number1').value); var number2 = parseInt(document.getElementById('number2').value); ,问题就解决了。

现在,有一个名为 DRY (Don't Repeat Yourself) 的编程最佳实践原则。任何时候你发现自己编写的代码已经写好了,你应该停下来重新思考你的方法。在多个地方复制代码是浪费你的时间,可能导致错误(如果你没有正确复制代码)并且不能很好地扩展(当你需要做出改变时,你必须做在几个地方也有同样的变化。)

让我们整合您的代码。在每个函数中唯一不同的是实际的数学运算符,所以不是编写函数4次,而是只需编写一次并根据运算符字段中提供的值进行正确的数学运算。

&#13;
&#13;
parseFloat()
&#13;
// Set up references to inputs, button and output just one time,
// instead of every time you have to do the math.
var num1 = document.getElementById('number1');
var num2 = document.getElementById('number2');
var oper = document.getElementById('operator');
var btn = document.getElementById("equals");
var out = document.getElementById("output");

// Set up event handler for the button
btn.addEventListener("click", function() {

  var result = null;  // Answer will go here
  
  // Do the right math based on the selected operator
  switch(oper.value){
    case "+":
      result = parseFloat(num1.value) + parseFloat(num2.value);
      break;
    case "-":
      result = parseFloat(num1.value) - parseFloat(num2.value);
      break;
    case "*":
      result = parseFloat(num1.value) * parseFloat(num2.value);
      break;
    case "/":
      result = parseFloat(num1.value) / parseFloat(num2.value);
      break;      
  }

  out.textContent = result;  // Output the result
});
&#13;
#operator { width: 3em;}
&#13;
&#13;
&#13;