0
I need to make a percentage calculation on a value, but depending on the number to be calculated (if there is a periodic decimation in the result for example) the result is rounded and has no decimal places, example:
(I’m calculating by 1.05 because it amounts to an addition of 105%)
11.114,37 * 1.05 = 11.6697 (Why no decimal places?)
11,11 * 1.05 = 11.55 (The correct would be 11.66)
That is... the calculation is wrong and sometimes there are not even decimals.. I need the correct results with only 2 decimal places. Where am I wrong? Please help me!
<html>
<head>
</head>
<body>
<form action="" method="">
<label for="usuario"> Valor: </label>
<input class="form-control" name="PO4R_ORCADO" id="PO4R_ORCADO" type="text" onblur="percentual();" >
<input class="form-control"name="PO4R_MAIS1" id="PO4R_MAIS1" type="text" >
</form>
</body>
</html>
<script type="text/javascript">
function percentual() {
var p105 = "1.05";
var p110 = "1.10";
var p115 = "1.15";
var vl_fr1 = document.getElementById("PO4R_ORCADO").value; //1.114,37
var a1 = parseFloat(vl_fr1)*parseFloat(p105);
//Multiplica 1.114,37 * 1.05
var a2 = parseFloat(vl_fr1)*parseFloat(p110);
var a3 = parseFloat(vl_fr1)*parseFloat(p115);
document.getElementById('PO4R_MAIS1').value = a1;
// resultado = 11.6697 na calcyladora do windows da 11.670,08
}
</script>
RESOLVED!!!
It follows the function for those who go through the same problem I do!
<script type="text/javascript">
function percentual() {
var p105 = "1.05";
var p110 = "1.10";
var p115 = "1.15";
var vl_fr1 = document.getElementById("PO4R_ORCADO").value;
vl_fr1 = vl_fr1.replace(/\./g,'').replace(',', '.');
var a1 = parseFloat(vl_fr1)*parseFloat(p105);
var a2 = parseFloat(vl_fr1)*parseFloat(p110);
var a3 = parseFloat(vl_fr1)*parseFloat(p115);
a1 = a1.toFixed(2);
document.getElementById('PO4R_MAIS1').value = a1;
}
</script>
This is because in this case there is more than 1 point in the string, I will edit the answer to also solve this case.
– Renato Diniz
Thank you very much for answering More still no decimals.. giving replace as suggested the value to be calculated was thus (11.114.37) The result was (11.6697) continues without decimals... continues the same problem :(
– A.Rangel
I edited the answer.
– Renato Diniz
Thank you You solved my problem!! Thank you very much
– A.Rangel
Show. If you can, mark the answer as you accept. Good luck.
– Renato Diniz