Calculation of JavaScript Multiplication

1

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>
    
asked by anonymous 22.05.2018 / 16:42

2 answers

2

The parseFloat will only understand numbers with the decimal separated by '.' rather than ','. ( link )

Before giving parseFloat, substitute the comma in the field by point:

vl_fr1 = vl_fr1.replace(',', '.');

And to get only two decimal places in the end result, use the toFixed () , passing the number of decimal places you want.

EDITED

That way it gets more complete, because if you have a string of type '11 .111.50 ', after replacing the comma it will be '11 .111.50', and parseFloat will also give a problem. The workaround is to replace the comma but also remove this second point:

vl_fr1 = vl_fr1.replace(/\./g,'').replace(',', '.');

And then just give the parseFloat. Remembering that toFixed rounds values, then '11670.0885' with two decimal places would be '11670.09'.

    
22.05.2018 / 17:26
-1

more or less this:

vl_fr1 = vl_fr1.replace(',', '.');
    
22.05.2018 / 17:33