Posted By

gatement on 07/23/09


Tagged

integer


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


Format an integer with commas


 / Published in: Ruby
 

Format an integer with commas to make it more readable

  1. def number_with_delimiter(number, delimiter=",")
  2. number.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
  3. end
  4.  
  5. a=1024
  6. p number_with_delimiter(a)

Report this snippet  

You need to login to post a comment.