1

Possible Duplicate:
Format numbers in javascript

I have a string represents a number: "3507654" I need to split it on a parts, by 3, e.g "3 507 654"

Number can be any value, but always a number.

Community
  • 1
  • 1
Alex Ivasyuv
  • 8,217
  • 17
  • 67
  • 89

1 Answers1

8

You could do the following:

"3507654".replace(/(\d)(?=(?:\d{3})+$)/g, "$1 ")

But this is not very efficient since it’s O(n2).

Gumbo
  • 620,600
  • 104
  • 758
  • 828