Format an integer with commas


/ Published in: Ruby
Save to your folder(s)

Format an integer with commas to make it more readable


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.