Calculation of toll per kilogram

Asked

Viewed 113 times

1

Hello, I need to do a toll calculation based on the kilogram. Each 100kg the toll value should 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 on 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

2 answers

2

I set a little 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 value of the Toll of 100kg.

Divide the weight by 100, to see how many 100 fits within 200 Kg, for example. Cabe 2, multiplying by the value of the toll of 100 Kg.

Then he checks if there is any REST left in the division by the %symbol. Then there’s nothing left.

But trade 200 for 101, or 201 and you’ll 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));
    }); 
});

  • worked, but I noticed a problem when the value is less than 100. See http://jsfiddle.net/zohfk3rq/

  • And what you want to happen when the weight is less than 100 ?

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

  • I changed the function.

  • Thank you very much diego, but it worked better here http://jsfiddle.net/3b7b5jzm/1/

  • You’re the boss, man. (y)

  • Your answer is fine, but I think your mistake lies in multiplying the toll by the rest. If there is rest, just include 1 toll value (ie: valor = valor + echoPedagio), because it means that the weight has already exceeded a multiple of 100 and a new toll should be added (from what I understand the question). It will be the case of the value 101, for example.

Show 2 more comments

1


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: http://jsfiddle.net/3b7b5jzm/

The example is applied to 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".

  • 1

    I made a small modification http://jsfiddle.net/3b7b5jzm/1/, thanks for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.