Remove , from all integers in rails

230 Views Asked by At

All integers are by default delimited by ',' .

For ex: 123456 is shown as 1,23,456.

Is there a way to remove ',' from all integers for all tables.

If it is for a single table and a particular field, we can do in the following way in its controller.

config.columns[:<int_field>].options={:i18n_options => {delimiter: ""}}

Is there a way to do this for all integer fields?

PS: Using Activescaffolding in my application.

Thanks.

3

There are 3 best solutions below

1
Shamsul Haque On

Use tr as:-

2.0.0-p645 :005 > "1,23,456".tr(',', '')
=> "123456"

Convert the result to integer as:-

2.0.0-p645 :005 > "1,23,456".tr(',', '').to_i
=> 123456

Check methods in string in irb as:-

"".methods

Use it in view as:-

<%= "1,23,456".tr(',', '').to_i %>

For more details see the document

1
Sven Koschnicke On

I assume Rails with Activescaffold uses formatting rules from its I18n module by default. You can change them in config/locales/en-US.yml. Here is the default one with the relevant lines highlighted.

This should affect all formatting of numbers in views. I think you just have to change the delimiter to a blank string:

...
format:
  delimiter: ""
...

You can find general information about Rails I18n in the corresponding I18n guide.

1
Mounika On

This worked perfectly.

Added the following lines in application_helper.rb:

def format_number_value(value, delimiter = '')
  if value.is_a? Integer
     ActiveSupport::NumberHelper.number_to_delimited(value, delimiter: '')
  else
    super
  end
end

Thanks.