Percentage calculation with JS

3

I am setting up a function to do the calculation using percentage. In my online test it works.

link

$(document).ready(function () {
        valorTotaldaNota    = "1.000,00";
        valorTotaldaNota    = valorTotaldaNota.replace(",", "");
        adValorem           = "0.33";
        gris                = "0.20";

        v1 = valorTotaldaNota * adValorem / 100;
        v2 = valorTotaldaNota * gris / 100;


        alert(  v1 + v2 );


});

But in my local file, the calculation is not correct and appears a lot of zero in the freight. Ex: 0.0087

$(document).ready(function () {
    $(".seguro").click(function() {

            valorTotaldaNota    = $("#valorTotaldaNota").val();
            valorTotaldaNota    = (valorTotaldaNota.replace(",", ""));
            adValorem           = ($("#adValorem").val());
            gris                = <?php echo $gris; ?>

            v1 = valorTotaldaNota * adValorem / 100;
            v2 = valorTotaldaNota * gris / 100;

            $("#valordoSeguro").val( v1 + v2 );

    }); 
});
    
asked by anonymous 15.05.2015 / 02:05

2 answers

2

I discovered the problem. One more conversion needed.

valorTotaldaNota    = valorTotaldaNota.replace(".", "");
valorTotaldaNota    = valorTotaldaNota.replace(",", ".");

First, take the point and then place the dot in the place of the comma.

Now it worked.

    
15.05.2015 / 03:24
2

When you have a string '1.000,00' and you do '1.000,00'.replace(",", ""); this will give

'1.00000' // tipo string

Fortunately, in JavaScript, '1.00000' * 5 gives 5. Although 1.00000 is a string and not a number. JavaScript does not use the comma, it only uses the dot to divide the decimal part. Then% w /% is actually read as 1.

How to do it then?

To do arithmetic operations you must be working with numbers, not strings. You can use a function to clear the entry and return a number. It must be able to handle both cases where the comma divides the decimal part as '1.00000' but also the other values that you set as 1.000,00 ...

function conversor(str){
    if (typeof str == 'number') return str;
    var nr;
    var virgulaSeparaDecimais = str.match(/(,)\d{2}$/);
    if (virgulaSeparaDecimais) nr = str.replace('.', '').replace(',', '.')
    else nr = str.replace(',', '');
    return parseFloat(nr);
}

A code hint would be:

function conversor(str) {
    if (typeof str == 'number') return str;
    var nr;
    var virgulaSeparaDecimais = str.match(/(,)\d{2}$/);
    if (virgulaSeparaDecimais) nr = str.replace('.', '').replace(',', '.')
    else nr = str.replace(',', '');
    return parseFloat(nr);
}

$(document).ready(function () {
    $(".seguro").click(function () {
        var valorTotaldaNota = conversor($("#valorTotaldaNota").val());
        var adValorem = conversor($("#adValorem").val());
        var gris = conversor( <? php echo $gris; ?> )

        var v1 = valorTotaldaNota * adValorem / 100;
        var v2 = valorTotaldaNota * gris / 100;

        $("#valordoSeguro").val(v1 + v2);
    });
});

Notice that you were omitting "0.33" in the variable declaration. Avoid this because you are exporting to global space and you can re-write some variables unintentionally.

The regex var looks for a comma (,)\d{2}$ followed by 2 digit (,) that are at the end of the string \d{2} . If your numbers have a variant decimal part the thing becomes more complex. In that case tell me to fix it here.

    
15.05.2015 / 06:36