简体   繁体   English

如何舍入一个输出步骤.5(javascript)并使用输出进行计算?

[英]How to round an output step .5 (javascript) and calculate with the output?

I am not very familiar with JavaScript and/or form calculations. 我对JavaScript和/或表单计算不是很熟悉。 The form and script I made after searching the internet about the topic. 我在网上搜索有关该主题的表格和脚本。

How can I round the output (output_abc) in 0.5 steps (1.0 1.5 2.0 … 7.7=7.5 or 7.8=8.0) and where to place it? 如何以0.5步(1.0 1.5 2.0…7.7 = 7.5或7.8 = 8.0)四舍五入输出(output_abc)以及将其放置在哪里? How can I (after rounding the output) [input_a] + [output_abc]? 如何(将输出舍入后)[input_a] + [output_abc]?

I am open to all suggestions, pointing directions, help. 我愿意接受所有建议,指出方向,提供帮助。

 (function() { function calcABC(input_a, input_b, input_c) { input_a = parseFloat(input_a); input_b = parseFloat(input_b); input_c = parseFloat(input_c); return (input_a + input_b + input_c).toFixed(1); } var SUM_ABC = document.getElementById("FORMINPUT"); if (SUM_ABC) { SUM_ABC.onsubmit = function() { this.output_abc.value = calcABC(this.input_a.value, this.input_b.value, this.input_c.value); return false; }; } }()); 
 <form id="FORMINPUT" action=""> <p><label for="input_a">input a</label> <input id="input_a" name="input_a" type="number" pattern="[1-9]" min="1" max="99" /></p> <p><label for="input_b">input b</label> <input id="input_b" name="input_b" type="number" pattern="[1-9]" min="1" max="99" /></p> <p><label for="input_c">input c</label> <input id="input_c" name="input_c" type="number" pattern="[1-9]" min="1" max="99" /></p> <p><input type="reset" value="Reset" /> <input type="submit" value="Calculate" /></p> <p><label for="output_abc">output abc</label> <output id="output abc" name="output abc" type="number"></output> </p> <p>Here I want to display [input_a + output_abc]</p> </form> 

What you can do is round your ouput_abc as 您可以做的是将ouput_abc取整为

Math.round(output.abc * 2)/2

It will give you the output in 0.5 steps 它将以0.5步为您提供输出

Rounding to 0.5, is very similar to rounding say to 2 decimal places etc. 四舍五入到0.5,非常类似于四舍五入到小数点后两位等。

Basically multiple by X, round, and then divide by X.. 基本上乘以X,先四舍五入,然后再除以X。

X = to the rounding spec. X =舍入规格。 So if you wanted to round to 1 decimal place for example -> 7.17 = Math.round(7.17 * 10) / 10 , this would of course give you 7.2 因此,如果您想四舍五入到小数点后7.17 = Math.round(7.17 * 10) / 10 ,例如-> 7.17 = Math.round(7.17 * 10) / 10 ,那么当然可以得到7.2

So to round by 0.5 , X will just equal 2.. 因此,要一轮0.5 ,X只会等于2 ..

eg. 例如。

Math.round(7.7 * 2) / 2 === 7.5

Maybe you can add after the line where you are calling the calcABC function but before that assign the return value from the function to a variable. 也许您可以在调用calcABC函数的行之后添加,但在此之前将函数的返回值分配给变量。


  function calcABC(input_a, input_b, input_c) {
    input_a = parseFloat(input_a);
    input_b = parseFloat(input_b);
    input_c = parseFloat(input_c);
    return (input_a + input_b + input_c).toFixed(1);
  }
  var SUM_ABC = document.getElementById("FORMINPUT");
  if (SUM_ABC) {
    SUM_ABC.onsubmit = function() {
      var outputValue = calcABC(this.input_a.value, this.input_b.value, this.input_c.value);
this.output_abc.value = Math.round(outputValue * 2)/2
      return false;
    };
  }
}());````

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

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