4 Replies Latest reply on Oct 16, 2017 3:53 PM by Clarkopolous

    Rounding / Calc error

    Clarkopolous

      I've having some trouble with either rounding or calculating (or both), values for a timesheet.

       

      I thought my coding was all correct, however it seems to be rounding UP.

       

      Example:

      Start time     Break     Finish time     Total hours

          8:30         15 mins         13:15            5.00         Total hours should be 4.50

       

      This is the coding I am using:

       

      var Break = getField("Break1").value;

       

      // start
      var start = getField("Start1").value;
      var startArr = start.split(":");

       

      // finish
      var finish = getField("Finish1").value;
      var finishArr = finish.split(":");

       

      //difference
      var hourDiff = Math.abs(finishArr[0] - startArr[0]);
      var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*100);

       

      if (minDiff.toString().length==1)
      minDiff = '0' + minDiff;

       

      var output = hourDiff + "." + minDiff;
      if((output==Infinity)||isNaN(output)){
      event.value="";
      } else {
      event.value = output-Break;
      }

       

      Could someone please assist where I've gone wrong?

       

      FYI - the break gives the value 0.25