Calculation of toll per kilogram

1

Hello, I need to make a toll calculation based on the kilogram. Every 100Kg toll value must be added

  

Ex:

     

50kg = 6.42

     

100kg = 6.42

     

101kg = 12.84

     

200kg = 12.84

     

299kg = 12.84

     

300kg = 19.26

     

...

$(document).ready(function () {
    $("#pesoTotaldaNota").focusout(function() {
        peso = "20.000"; // = 20Kg
        echoPedagio = "6.42"        
    }); 
});

I need to show the result in input

<input name="pedagio" type="text" class="form-control" id="pedagio" value="" required>

Another example Até 100kg = 6.42 Até 200kg = 6.42+6.42 Até 200kg = 6.42+6.42+6.42

    
asked by anonymous 15.05.2015 / 14:06

2 answers

1

Another alternative:

$(document).ready(function () {
    $("#pedagio").change(function () {
        var peso = parseFloat(this.value);
        var pedagio = 6.42;
        var extra = Math.ceil(peso / 100) * pedagio;
        this.value = peso + extra;
    });
});

jsFiddle: link

The example is applied to the input. What it does is know how many times the weight is greater than 100 and round to multiply by pedagio . This value is then added to the final result as an "extra".

    
15.05.2015 / 14:33
2

I set a small example for you:

<input name="pedagio" type="text" class="form-control" id="pedagio" value="" required>
<a id="pesoTotaldaNota">Calcular</a>

In this function it has the weight of 100Kg and the Toll value of 100Kg.

Divide the Weight by 100, to see how many 100 fits within 200 kg, for example. Fits 2, multiplying by the toll value of 100 kg.

Then it checks to see if there is a REST in the split by the% symbol. In this case there is nothing left.

But change the 200 by 101, or 201 and you will see the result.

$(document).ready(function () {
    $("#pesoTotaldaNota").click(function() {
        peso = "101";
        var valor = 0;
        var resto = 0;
        echoPedagio = "6.42";
        
        if(peso >= 100){
            valor = (peso / 100) * echoPedagio;
            resto = peso % 100;
            if(resto > 0)
            valor = (echoPedagio * resto) + valor;
        }
        else
            valor = 6.42;
        
        $('#pedagio').val(valor.toFixed(2));
    }); 
});
    
15.05.2015 / 14:22