Number Formatting - Add Commas

by patelsan

CoffeeScript

FormatNumber = (nStr) ->
 nStr += ''
 x = nStr.split('.')
 x1 = x[0]
 x2 = if x.length > 1 then '.' + x[1] else ''
 rgx = /(\d+)(\d{3})/
 while(rgx.test(x1))
  x1 = x1.replace(rgx, '$1' + ',' + '$2')
 return x1 + x2
 
for i in [1..8]
 console.log FormatNumber(5 * Math.pow(10,i))