Division greater than R $ 999.00 results in NaN. How to format correctly

6

Division smaller than $ 999.00 works correctly. However, greater than $ 999.00 results in NaN. How to stir it? I already put a replace and nothing!

$(document).ready(function() {
   var demo1 = $('span.ecwid-productBrowser-price-value');
   demo1.each(function() {
   var valor1 = parseInt(this.innerHTML.replace(',', '.').substr(2) / 3, 10);
   $(this).append("<p><i>dividido por 3 é: " + valor1 + "</i></p>")
    })
  demo1.append();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>

<body>

<span class='ecwid-productBrowser-price-value'>R$800,00</span><hr>
<span class='ecwid-productBrowser-price-value'>R$999,00</span><hr>
<span class='ecwid-productBrowser-price-value'>R$1.000,00</span><hr>
<span class='ecwid-productBrowser-price-value'>R$1.200,00</span><hr>


</body>
    
asked by anonymous 01.09.2017 / 20:57

2 answers

7

The problem is that there are two points when the number is in the order of a thousand. That is, it looks like this: parseInt(1.200.00, 10) and this is a bug.

You need to remove the dots before converting the comma into a dot.

$(document).ready(function() {
   var demo1 = $('span.ecwid-productBrowser-price-value');
   demo1.each(function() {
   var valor1 = parseInt(this.innerHTML.replace(/\./g, '').replace(',', '.').substr(2) / 3, 10);
   $(this).append("<p><i>dividido por 3 é: " + valor1 + "</i></p>")
    })
  demo1.append();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>

<body>

<span class='ecwid-productBrowser-price-value'>R$800,00</span><hr>
<span class='ecwid-productBrowser-price-value'>R$999,00</span><hr>
<span class='ecwid-productBrowser-price-value'>R$1.000,00</span><hr>
<span class='ecwid-productBrowser-price-value'>R$1.200,00</span><hr>


</body>
    
01.09.2017 / 21:01
2

Remove the points of the thousand

$(document).ready(function() {
   var demo1 = $('span.ecwid-productBrowser-price-value');
   demo1.each(function() {
   var valor1 = parseInt(this.innerHTML.replace(',', '.').substr(2) / 3, 10);
   $(this).append("<p><i>dividido por 3 é: " + valor1 + "</i></p>")
    })
  demo1.append();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>

<body>

<span class='ecwid-productBrowser-price-value'>R$800,00</span><hr>
<span class='ecwid-productBrowser-price-value'>R$999,00</span><hr>
<span class='ecwid-productBrowser-price-value'>R$1000,00</span><hr>
<span class='ecwid-productBrowser-price-value'>R$1200,00</span><hr>


</body>
    
01.09.2017 / 21:06