How to fix incorrect division in JS?

  • 0
    Hello friends!
    I'm making myself an insulin sensitivity calculator.
    500 divided by the daily insulin dose 28 = 17.86
    I divide 17.86 by 12 and should get 1.49 (rounded) and get 1.42

    Please tell me what's wrong ??

    Here is the script.
    $(function() {    
      $('input').keyup(function(){
        updateTotal(); 
      });   
    
    var updateTotal = function () {
      var input2 = parseInt($('#tab_id2').val()) || null;
      var total = (500 / input2).toFixed(2);
      if (input2) {        
          $('#total_id_1').val(total);
    	  
        };
    
      var input1 = parseInt($('#total_id_1').val()) || null;
      var total = (input1 / 12).toFixed(2);
      if (input1) {        
          $('#total_id_2').val(total);  
        }
    	
      } 
    });

    500÷<input type="number" id="tab_id2" />=
    <input type="number" id="total_id_1"/>гр. <br>
    <input type="number" id="total_id_2" />ХЕ


    And another question, is it possible to make it so that when you delete a value from input, all other values ​​are deleted?
    Thanks a lot in advance!
    JavaScript Anonymous, Nov 21, 2019

  • 2 Answers
  • 0
    parseInt rounds to an integer, parseFloat rounds to a fractional (float)



    https://jsfiddle.net/okfp3j4s/



    Anonymous

  • 0
    Not that you first convert to intu, and then wait for accuracy by giving it a float.





    var input1 = parseInt ($ ('# total_id_1'). val ()) || null;
    Grace Estrada

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!