Computation of Multiplication in Javascript

Asked

Viewed 289 times

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>

2 answers

2


parseFloat will only understand numbers with decimal box separated by '.' instead of ','. (https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseFloat)

Before giving parseFloat, replace the comma typed in the field by a dot:

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

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

EDITED

This way it is more complete, because if you have a string of type '11.111,50', after replacing the comma will be '11.111.50', and parseFloat will also give problem. The solution is to replace the comma but also remove this second point:

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

And only then give the parseFloat. Remembering that toFixed rounds values, then '11670,0885' with two decimal places would be '11670,09'.

  • 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.

  • 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 :(

  • I edited the answer.

  • Thank you You solved my problem!! Thank you very much

  • Show. If you can, mark the answer as you accept. Good luck.

-1

more or less that:

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

Browser other questions tagged

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