0

I want this :

"You won 50000000 dollars" ==> "You won 50,000,000 dollars"

or

"Can you jump 20000 times in 340000 meters?" ==> "Can you jump 20,000 times in 340,000 meters?"

My solution is to split the words by space character and check all words separately. Is there a better way ?

Dave
  • 498
  • 2
  • 7
  • 22
Bojbaj
  • 472
  • 1
  • 6
  • 15
  • no, that's how to format a number, i want a way to format all numbers in a string that i don't know count or numbers position in string... first at all need to found out numbers and then use one of this codes. – Bojbaj Aug 16 '15 at 12:49
  • That is what that function does as well. `"hello 10000 world 20000000 foo".replace(/\B(?=(\d{3})+(?!\d))/g, ",");` => `"hello 10,000 world 20,000,000 foo"` – tombruijn Aug 16 '15 at 12:53

1 Answers1

0

To split lines problem I often use here is a regular expression

input.toString().replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g,'$1 ')

But for your case it can Preobrazovatel in view

input.toString().replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g,'$1,')

I checked it out on this line

input=50000000
input.toString().replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g,'$1,')

The result was

"50,000,000"

that is, it works exactly as You need

Alexey Popov
  • 76
  • 2
  • 8