0
I wonder how I can accomplish the sum but eliminating or disregarding the "R$", transforming into number.
Values in R$ are automatically generated by the system.
Follows the code:
<html><head>
<script type="text/javascript">
window.onload = function (){
document.querySelector('div.de').id = 'de';
document.querySelector('div.por').id = 'por';
document.querySelector('div.economize').id = 'economize';
var Div = document.createElement("div");
document.body.appendChild(Div) ;
Div.id = 'resultado';
var Campo1 = document.getElementById("de") .innerHTML ;
var Campo2 = document.getElementById("por") .innerHTML ;
var Campo3 = document.getElementById("economize") .innerHTML ;
var subtracao = eval(parseInt(Campo3) / parseInt(Campo1) * 100);
document.getElementById('resultado').innerHTML = subtracao.toFixed(1)+"%" ;
}
</script>
</head>
<body>
<div class="de"> R$ 266,00 </div>
<div class="por"> R$ 145,36</div>
<div class="economize"> R$ 120,64</div>
</body></html>
<div class="de"> R$ 266,00 </div> <div class="por"> R$ 145,36</div> <div class="save"> R then 120,64</div>
– lucas inverso
Can you take that
eval
, is unnecessary and will only slow down the code. When you see aeval
on js out there, 99% chance of being unnecessary! :)– bfavaretto
Thanks for the tip, hugs!
– lucas inverso
Just a few details, you’re doing
parseInt
(useparseFloat
), Yeah, it’ll remove the pennies. If the number has a thousand separator"."
remove them before making the paseFloat..innerHTML.replace('R$', '').replace('.','').replace(',','.')
– Leandro Amorim