Problem when calculating percentage using parseFloat and toFixed

Asked

Viewed 145 times

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. inserir a descrição da imagem aqui

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:

inserir a descrição da imagem aqui

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

  • You need to remove the dots and swap commas for dots before you pass parseFloat in the field values.

2 answers

1

function to swap characters

function replaceAll(str, find, replace) {
    return str.replace(new RegExp(find, 'g'), replace);
}

how to call function

replaceAll('20,00',',','.');

another simple way

variavel.replace(/,/g, '.');
  • Solved 50% of the problem @Willian, now the calculation is done correctly up to R $ 999,99 and with the correct values of the decimals, but when I put a cost price being R $ 100,00 and a sale price being R $ 1000,00 it calculates -99,00% profit margin.

  • https://ideone.com/leEW4R @J.Thatcher ae how I did... next the error ta no . of the 1,000.00 and just remove it, put a replace '.', 'for your input values

  • if there is an error in the profit margen pass to string, then remove the exact uqe and return to float

0


Can also be used split and join to convert the fields.

I put snippet to illustrate.

function calculo() {

var pv = parseFloat($("input[name='iptPrcVendaProdutoEditar']").val().split('.').join('').split(',').join('.'));

var pc = parseFloat($("input[name='iptPrcCustoProdutoEditar']").val().split('.').join('').split(',').join('.'));

$("input[name='iptMargemLucroProdutoEditar']").val(

(!isNaN(pv) && !isNaN(pc) ?

((pv - pc) / pc * 100).toFixed(2) + '%' :

'')

);

};

$('input').not(':last').on('keyup', function() { calculo(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Preço venda: <input type="text" name="iptPrcVendaProdutoEditar" /><br /><br />
Preço custo: <input type="text" name="iptPrcCustoProdutoEditar" /><br /><br />
Margem lucro: <input type="text" name="iptMargemLucroProdutoEditar" readonly="readonly" />

Browser other questions tagged

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