There are 2 input number fields, using javascript, the value entered in one overwrites and displays the value in another field

<input id="goods" type="number" oninput="makePrice();"> <input id="price" type="number" oninput="makeGoods();"> function makePrice() { var temp = document.getElementById("goods").value; var price = temp * 0.14;//Здесь может быть другой коэффициент document.getElementById("price").value = price; } 

The function MakeGoods () is similar (if the user wants to buy a product, at some price, and not by the number of goods)
As in the input id = price field, make a value with a floating point so that after the comma the price in kopecks, even if an integer is obtained, it doesn’t matter that it displays a number, for example 100.00.
In the input id = goods field, on the contrary, when entering a price, to round up to a whole number of product units.
How to do it, with the help of JS, or HTML, point to the correct idea.

  • Math.round to help. there is nothing difficult - splash58
  • Understood, looking, thanks - wiaim

1 answer 1

toFixed you to help.
I did not understand exactly what was needed, but ... like this:

 document.addEventListener('DOMContentLoaded', e => { document.querySelector('#goods').addEventListener('input', makePrice); document.querySelector('#price').addEventListener('input', makeGoods); }); function makePrice(){ let temp = document.querySelector("#goods").value, factor = 0.14; document.querySelector("#price").value = (temp * factor).toFixed(); } function makeGoods(){ let temp = document.querySelector("#price").value, factor = 0.36; document.querySelector("#goods").value = (temp * factor).toFixed(2); } 
 <input id="goods" type="number" /> <input id="price" type="number" />