-2

Possible Duplicate:
Format numbers in javascript
How to print a number with commas as thousands separators in JavaScript

I want to separate the zeros of the gap in the price list.

As example

current: 150000 wanted: 150 000

current: 2730000 wanted: 2 730 000

Using jQuery and all browsers (Chrome, Firefox, IE, Opera)

UPD I want to dynamically add spaces when entering numbers

Step by step: user entered 10, in input 10
100 - 100
1000 - 1 000
10000 - 10 000
100000 - 100 000
1000000 - 1 000 000

UPD I used regex val.replace(/(\S)(?=(\S\S\S)+(?!\S))/g, "$1 "); It's correct works in FF only

Community
  • 1
  • 1
Stack Stack
  • 147
  • 1
  • 1
  • 11

1 Answers1

1

If n is your number, use following regex:

n.toString().replace( /\B(?=(\d{3})+(?!\d))/g, ' ' );
Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
closure
  • 7,412
  • 1
  • 23
  • 23
  • I don't understand that regex, but it seems to work `:)` – Šime Vidas Dec 13 '12 at 14:08
  • Doesn't work! 1 2 3 4 5 6 789 – Stack Stack Dec 14 '12 at 05:11
  • @StackStack quite surprised that it doesn't work for you. I tested this with latest versions of Safari, FF, Chrome, Opera on Mac. Works in all. Check the jsbin http://jsbin.com/umored/2/watch Suspect it has to do with some version of IE. – closure Dec 14 '12 at 07:16