0

I inserted this code here in a module

function execute_foreach(array){
    array.forEach(element => {
       let valor = parseInt(element.innerText);
       element.innerText = valor.toLocaleString('pt-BR')
    })
}

I put to insert the point automatically in the values. But if I enter a point value of "3,000", its return will be "3". How do I solve this? I have researched and don't find it. Is there another way to do this?

Bjorn Ruiz
  • 546
  • 2
  • 4
  • 15
  • `parseInt()` is for integers. Use `parseFloat()` for numbers with decimals. And see https://stackoverflow.com/questions/7571553/javascript-parse-float-is-ignoring-the-decimals-after-my-comma for converting the comma to a decimal point. – Barmar Jul 27 '21 at 18:49
  • This doesn't solve it, I don't even know why I put that parseInt – Bjorn Ruiz Jul 27 '21 at 18:51
  • What doesn't solve it? `valor = parseFloat(element.innerText.replace(',', '.'))` doesn't work? – Barmar Jul 27 '21 at 18:54
  • Is that supposed to be `3.000` or `3000`? – Barmar Jul 27 '21 at 18:54
  • Use `.replace(/,/g, '')` if you just want to remove the commas. – Barmar Jul 27 '21 at 18:55
  • Parsing back numbers formatted as human-readable for some locale is tricky, and more trouble than it’s worth if you have an alternative – for example, a `data-value` attribute with no thousands/other separators (and a `.` as a decimal point, if applicable). Then you can parse that with `Number(element.getAttribute('data-value'))`. – Ry- Jul 27 '21 at 23:18

0 Answers0