1

HI

Is there a way i can add commas to a number like so:

num= 1234567

then becomes

num = 1,234,567

I would like to make this call in my helper i.e module

thanks

Mo.
  • 40,243
  • 37
  • 86
  • 131

1 Answers1

11

Use number_with_delimiter.

From your helpers:

number_with_delimiter(12345678)       # => 12,345,678

UPDATE: The code for the method.

jpemberthy
  • 7,473
  • 8
  • 44
  • 52
  • 2
    Yes, But you could include the module, or, if you don't want(like) to include views helpers outside, here's the code for the `number_with_delimeter` method http://gist.github.com/484764 – jpemberthy Jul 21 '10 at 16:58
  • 1
    Why would you ever want to do this outside of a view? It's purely visual, and provides no extra information about the value itself. – jdl Jul 21 '10 at 17:02
  • 1
    @jdl, I would use this helper outside the views in following situations: logging, tracing, flash messages etc. etc. – Harish Shetty Jul 21 '10 at 18:18
  • +1 for cribbing the code for number_with_delimiter so that you don't have to include all of the view helpers. – sosborn Jul 22 '10 at 00:24
  • @KandadaBoggu Ah, gotcha. I would call all of those "views" but I can see that in the context of a Rails app you'd still need to require explicit access to the helper outside of the official "view". – jdl Jul 22 '10 at 16:08
  • +1 for including the code, so that I can port this to a ruby script. thanks – baash05 Mar 11 '14 at 21:57