Problem with jquery percentage calculation

Asked

Viewed 55 times

0

Good afternoon , I’m having trouble calculating percentage, but the error does not occur in the mile up, anyone has any idea what may be?

var cost_value = $('#cost_value').val().replace('.', '').replace(',', '.');
var unitary_value = parseFloat(cost_value) + (parseFloat(cost_value) / 100 * 
50.00);
console.log(unitary_value);
console.log(number_format(unitary_value, 2, ',', '.'));

Xenon

cost_value entrada:500000.00
unitary_value saida:750.000,00
cost_value entrada:5000.000.00
unitary_value saida:7.500,00

1 answer

2

let’s first understand what each function does and what it returns.

.val() jQuery will return the value of the element which in this case will be a string, according to the documentation w3schools the string.replace("","") (Javascript function) it replaces only the first element found in the string, in your case when you enter the million house it has 2 points [1.000.000,00].

Solution: the string.replace("","") accept regex, use the following .replace(/\\./g,"")

  • thanks saved me, I did not know that I only removed the first, I managed with another way, but I did not want to gambiarra, I will read more documentation from now.

Browser other questions tagged

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