This question has already been answered:

<script> function mp() { document.getElementById('mpol').value = (parseFloat(document.getElementById('kolrap').value) * parseFloat(document.getElementById('height').value)|| 0).toFixed(0) ; } document.getElementById('kolrap').addEventListener('input',mp); document.getElementById('height').addEventListener('input',mp); </script> 

When displaying the answer, for example, it will be 345354. It is necessary that 345 354 space be output after the "thousands" so to speak

Reported as a duplicate by Grundy , aleksandr barakin , pavel , Visman , cheops participants on Oct 18 '16 at 5:17 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • As an option, use numeraljs.com or any other library on this topic, but if the task requires you to do it in one place, then I think it’s better to write it manually. - Vyacheslav Danshin
  • why the library, regex perfectly divides - nick_n_a

1 answer 1

After each character, followed by a multiple of three (and non-zero) the number of digits, we add a space:

 ("" + 123456789).replace(/.(?=(\d{3})+$)/g, "$& ") 
  • And why use $ substitution? It is possible and just "". - nick_n_a
  • @nick_n_a, you can't. Give an example of a replacement, I will give a counterexample. - Qwertiy
  • @nick_n_a, because the regulars captures 1 character. And the solution given in the answer does not work for numbers with decimal places. - Visman
  • not quite clear code. - Big Daddy
  • one
    @nick_n_a, like this: ("" + 123456789).replace(/(?!^)(?=(\d{3})+$)/g, " ") , but I don’t see what it would be it is better. - Qwertiy