0

<!DOCTYPE html>
<html lang="en">
  <head> </head>

  <body translate="no">
    <input type="number" placeholder="user enter's here" name="price" id="get" />

    <input type="number" name="reduced" value="0.0039" id="qty_1" />

    <input type="number" placeholder="output" name="total" id="send" readonly />

    <script src="https://cpwebassets.codepen.io/assets/common/stopExecutionOnTimeout-1b93190375e9ccc259df3a57c1abc0e64599724ae30d7ea4c6877eb615f89387.js"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
   <script id="rendered-js">
  $(function () {
    $("#get, #reduced").keyup(function () {
      var input = $("#get").val() || 0;
      var input2 = $("#reduced").val() || 0;
      $("#send").val(input - input2);
      
    });
  });
  //# sourceURL=pen.js
</script>
  </body>
</html>

i do have a code which substract the amount user enter's and when user enter an decimal number it will show very long number how can i short it to two places 39409.44 i tried adding .tofixed(2) and other didn't work

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • Question has been answered before: https://stackoverflow.com/a/12830454/18371622 – t0ru Jul 20 '22 at 13:00
  • where should i add the .fixed(2) i tried adding it didin't work –  Jul 20 '22 at 13:01
  • Does this answer your question? [Format number to always show 2 decimal places](https://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places) – gre_gor Jul 22 '22 at 08:35

2 Answers2

1

You can use toFixed()

//...
var price = parseFloat($("#price_1").val()).toFixed(2);
var qty = parseFloat($("#qty_1").val()).toFixed(2);
//...
0

Try the below code, it also takes into consideration the scenario where input has invalid number entered as well:

$("#get, #reduced").keyup(function () {
  var input = parseFloat($("#get").val()) || 0.0;
  var input2 = parseFloat($("#reduced").val()) || 0.0;

  var result = parseFloat(input - input2).toFixed(2)

  $("#send").val(result);
  
});

Hope it helps.

Payal
  • 170
  • 6