views:

91

answers:

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

+5  A: 

Use number_with_delimiter.

From your helpers:

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

UPDATE: The code for the method.

jpemberthy
i had the notion that this only worked in the views?
Mo
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
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
@jdl, I would use this helper outside the views in following situations: logging, tracing, flash messages etc. etc.
KandadaBoggu
+1 for cribbing the code for number_with_delimiter so that you don't have to include all of the view helpers.
sosborn
@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