0

I want when user enter a number into input field like this 54.78987, or any number more than two decimal places, it should automatically go back to two decimal places like 54.78. Function can be use on keyup or onchange event.

  • 2
    What have you tried so far? What issues are you running into? – LW001 Aug 30 '21 at 12:19
  • I did not use any code still , want when user enter more than two decimal value in html form input. after leaving or on unfocused input value should reduced to two decimal places. any help will be appreciated. thanks – onlinegsali Aug 30 '21 at 12:22
  • Look into using a [change event](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event) on the input and use that to edit its value. – LW001 Aug 30 '21 at 12:26
  • It cant figure out. I need the implementation of codes. onchnge event how i will reduce the vallue. I need some codes which capture reduce the user entered value. – onlinegsali Aug 30 '21 at 12:31
  • Or [In jQuery, what's the best way of formatting a number to 2 decimal places?](https://stackoverflow.com/q/477892/215552) – Heretic Monkey Aug 31 '21 at 15:33
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 01 '21 at 13:24
  • I got the solution from above provided link. Thanks. – onlinegsali Sep 01 '21 at 19:42

1 Answers1

-1

use parseFloat.ToFixed(2) for two decimal call.

$(document).ready(function(){
 
  $("#inpt").on('blur',function(){
      var currentObj=$(this);
      var currentVal= currentObj.val();
      if(!isNaN(currentVal)){
        var updatedVal=parseFloat(currentVal).toFixed(2);
        currentObj.val(updatedVal);
      }
      else{
          currentObj.val("");
      }
 })


})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="inpt"/>
Negi Rox
  • 3,828
  • 1
  • 11
  • 18