0
function totalrunningtime(){
        var sum = 0;
        var qty = document.getElementById("cutterprodqty").value;
            $(".cutterrumnoofmin").each(function() {
                                                                //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum += parseFloat(this.value);
            }
            });
                                                            //.toFixed() method will roundoff the final sum to 2 decimal places
            $("#cutterrunningtimetotal").html(sum);
            document.getElementById("cutterrunningtimetotal").value =  sum;

            var total = (sum/qty);
            var t = total.toFixed(3);
            $("#cutterspeed").val(t);


}

When i input on textfields ex. 420/600 the answer is 0.700, when I checked in calculator the answer is 0.7

Please help out, many thanks :)

Benj Gonzales
  • 47
  • 2
  • 7

1 Answers1

0

Since you are using Number#toFixed it will automatically generate the 3 digits after the decimal part(0.700 is equivalent to 0.7).

To avoid the 0 at the trailing of decimal part use String#replace method.

var t = total.toFixed(3).replace(/(\.[^0]*)0+/, function(_, m1){
   // check all are 0 or not 
   return m1.length > 1 ? m1 : '';
})

Regex explanation here.


UPDATE : Or far better way by parsing the result string as @JJJ's comment.

var t = Number(total.toFixed(3))
//  convert into string using toString() if necessary

Refer : Remove insignificant trailing zeros from a number?

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
  • 1
    Not my downvote, but this doesn't work ("7" becomes "7.") and regex is overkill. (`var t = +total.toFixed(3);`) – JJJ Jun 02 '17 at 06:27