Convert String from a div to int and do a math operation

1

I'm trying to do the following function:

    <span id="our_price_display">R$ 71,90</span>

    function calculaParcela(){
    var regex = /\d+,\d+/g;
    var texto = $(".our_price_display").text(); //id da div com o texto R$ 71,90
    var valor = regex.exec(texto);
    var insereValor = $("#valor-parcelado").text(valor.join(""));

    console.log(insereValor);

    var divide = insereValor / 3;

    console.log(divide); 
}

But on the console it returns me NaN.

What am I missing and what could be done to correct this operation?

    
asked by anonymous 24.11.2016 / 20:56

1 answer

2

NaN is an error returned to non-numeric (not a number). You will have to convert to the number using parseFloat :

$("button").click(function() {
  $("#resultado").text(calculaParcela());
});

function calculaParcela() {
  var regex = /\d+,\d+/g;
  var texto = $("#our_price_display").text(); //id da div com o texto R$ 170
  var valor = regex.exec(texto);
  var insereValor = parseFloat(valor.join("").replace(",","."));
  var divide = insereValor / 3;
  return divide;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><spanid="our_price_display">R$ 71,90</span>
<br/>
<span id="resultado"></span>
<br/>
<button>Só valor</button>
    
24.11.2016 / 21:04