0

I'm struggling with putting a dot after each 3 numbers in JavaScript.

I know that it should be done using regex and I should use join method? But can someone please tell me how to implement that?

My idea for now is to do like that:

let num=12312;
let res=num.replace(/(.{3}/g,".");
console.log(res);

But that's not working. Thank you in advance!

Martinez
  • 65
  • 5

1 Answers1

-2

You can use toLocaleString to add comma after 3 number.

var x = 100000000;
console.log(x.toLocaleString('EN-US'))
Rahul Sharma
  • 9,534
  • 1
  • 15
  • 37
  • Just curious, how did you manage post an answer to a closed question after it was closed? – vanowm May 09 '22 at 11:15
  • 2
    @vanowm https://meta.stackexchange.com/questions/91922/how-was-this-answer-posted-after-this-question-was-closed – adiga May 09 '22 at 11:16
  • 1
    Try that with `1000` instead of `100000000`. Your code puts the character **before** every third character from the **right**, not after every third character from the left. Also you picked a US locale so its the wrong character. It only works in your example because the number of digits in the number is divisible by three. – Quentin May 09 '22 at 11:17
  • @vanowm I was typing the answer. I didn't know It's closed. – Rahul Sharma May 09 '22 at 11:17