简体   繁体   中英

JavaScript calculator writes wrong number

I have a small error in this code, please help me.

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, 
    maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <input type="text" id="price">
  <button onclick="calc()">GO</button>
  <h1 id="show"></h1>
  <script type="text/javascript">
    function calc() {
      "use strict";
      var price = document.getElementById('price').value;
      var res = (price / 100 * 5 + 20) + price;
      var show = document.getElementById('show').value = Math.floor(res);
    }
  </script>
</body>
</html>

ex:write 100 in input the result is 10025, I need 125

It's because you try to add String to a Number. You need to convert price to a Number like this :

var price = parseFloat(document.getElementById('price').value);
// Or like this :
var price = Number(document.getElementById('price').value);
// Or like this :
var price = document.getElementById('price').value * 1;

Full example which shows decimal numbers:

 var priceElement = document.getElementById('price'); var showElement = document.getElementById('show'); function calc() { var price = parseFloat(priceElement.value, 10); var result = (price / 100 * 5 + 20) + price; showElement.innerHTML = result.toFixed(2); } 
 <input type="text" id="price"> <button onclick="calc()">GO</button> <h1 id="show"></h1> 

Quite there.


A couple of fixes:

Store your elements outside of the function, since their id s won't change in your case:

var priceElement = document.getElementById('price');
var showElement = document.getElementById('show');

Use parseFloat(...) to parse floating point numbers stored in strings:

var price = parseFloat(priceElement.value);

To set an element's content (in your case, the content of the h1 element), use .innerHTML :

showElement.innerHTML = Math.floor(result);

 var priceElement = document.getElementById('price'); var showElement = document.getElementById('show'); function calc() { var price = parseFloat(priceElement.value); var result = (price / 100 * 5 + 20) + price; showElement.innerHTML = Math.floor(result); } 
 <input type="text" id="price"> <button onclick="calc()">GO</button> <h1 id="show"></h1> 

Yes, the value of price is string, so convert it to number.
And I don't think your "document.getElementById('show').value" is useful.
And the variable show is not used.
And the formula for res is somewhat convoluted -- see var v1.
Maybe you will find using console.log's useful in debugging.

<html>
<body>
<input type="text" id="price">
<button onclick="calc()">GO</button>
<h1 id="show"></h1>
<script type="text/javascript">
  "use strict";
function calc() {
  var price = 1*document.getElementById('price').value;
  console.log("price", price);
  var res = (price / 100 * 5 + 20) + price;
  console.log("res", res);
  document.getElementById('show').innerHTML = Math.floor(res);
  var v1 = price*1.05 + 20;
  console.log("v1", v1);
  document.getElementById('show').innerHTML += ", " + v1;
}
</script>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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