I need to do a percentage calculation on a value, but depending on the number to be calculated (if there is periodic decimal in the result for example) the result is rounded and does not display decimal places, / p>
(I am calculating by 1.05 because it equals an increase of 105%)
11.114,37 * 1.05 = 11.6697 (Why no decimals?)
11.11 * 1.05 = 11.55 (The correct one would be 11.66)
I mean ... The calculation is wrong and sometimes no decimal places appear .. I need the correct results with only 2 decimal places. Where am I going 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>
SOLVED !!!
Follow the function for anyone who has the same problem as me!
<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>