Question: I need a way to format numbers. I stored some numbers in my DB table, e.g. `12500`, and would like to print them in this format `12 500` (so there is a space every 3 digits). Is there an elegant way to do this?

Answer: 
Activesupport uses this regexp (and no reverse reverse).

```
10000000.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1 ") #=> "10 000 000"

```
All but one of the answers use `n.to_s`. @MrMorphe's does not, but he creates an array to be `join`ed. Here's a way that uses neither [Fixnum#to\_s](http://ruby-doc.org//core-2.2.0/Fixnum.html#method-i-to_s) nor [Array#join](http://ruby-doc.org//core-2.2.0/Array.html#method-i-join).

```
def separate(n,c=' ')
  m = n
  str = ''
  loop do
    m,r = m.divmod(1000)
    return str.insert(0,"#{r}") if m.zero?
    str.insert(0,"#{c}#{"%03d" % r}")
  end
end

separate(1)       #=>         "1"
separate(12)      #=>        "12"
separate(123)     #=>       "123"
separate(1234)    #=>     "1 234"
separate(12045)   #=>    "12 045"
separate(123456)  #=>   "123 456"
separate(1234000) #=> "1 234 000"

```

Hmmm. Is that column on the right tipping?

Another way that uses `to_s` but not `join`:

```
def separate(n, c=' ')
  str = n.to_s
  sz = str.size
  (3...sz).step(3) { |i| str.insert(sz-i, c) }
  str
end

```