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,585
  • 17
  • 72
  • 90

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
  • 643,351
  • 109
  • 780
  • 844