Calculate/Multiply/Sum without "R$"

Asked

Viewed 484 times

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>

  • 1

    Can you take that eval, is unnecessary and will only slow down the code. When you see a eval on js out there, 99% chance of being unnecessary! :)

  • Thanks for the tip, hugs!

  • 1

    Just a few details, you’re doing parseInt (use parseFloat), Yeah, it’ll remove the pennies. If the number has a thousand separator "." remove them before making the paseFloat. .innerHTML.replace('R$', '').replace('.','').replace(',','.')

1 answer

1


You can use replace this way:

var Campo1 = document.getElementById("de").innerHTML.replace('R$', '').replace(',','.');
var Campo2 = document.getElementById("por").innerHTML.replace('R$', '').replace(',','.');
var Campo3 = document.getElementById("economize").innerHTML.replace('R$', '').replace(',','.');
  • Thank you very much Edgar,

  • If this resolves, don’t forget to mark as answer :) @lucasinverso

  • @lucasinverso And if you vote, if you think it’s a good answer

Browser other questions tagged

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