1
I want to do something very simple, but as simple as it is, I’m not getting it. I have an input that receives the cost value, an input that calculates and shows the profit margin in % and an input that receives the sales price.
The question is as follows, the way I got it today, the calculation is done correctly only up to the amount (R$ 999.99), when it reaches the thousands, the calculation of a wrong value, for example:
And on top of everything else, the calculation ignores the decimals. If I put a sale price being R $ 199,00 of the same profit margin that I put R $ 199,99, ie, 199,90 he sees as 199 and 199,00 he also sees as 199. It seems to be something so simple and I’m not able to do it.
Code I currently have:
var precoVenda = parseFloat($("input[name='iptPrcVendaProdutoEditar']").val());
var precoCusto = parseFloat($("input[name='iptPrcCustoProdutoEditar']").val());
var margemLucro = parseFloat((((precoVenda - precoCusto)/precoCusto)*100)).toFixed(2);
$("input[name='iptMargemLucroProdutoEditar']").val(margemLucro.replace('.',','));
I had a similar problem https://answall.com/questions/308754/behaviorsstrangenessno-javascript, try using this lib https://github.com/dtrebbien/BigDecimal.js
– Wictor Chaves
You need to remove the dots and swap commas for dots before you pass parseFloat in the field values.
– bfavaretto