Search code examples
rubylocalizationfloating-pointcamping

localize floating point in camping web app


I have a ruby camping web app where user inputs floats. However in our country floating point delimiter is comma instead of period.

My question is how to implement this change?

User inputs 12,5 which should be changed to 12.5 before saving to DB. Similarily when the user calls up a report, floats should be displayed with commas.

Should I implement my own method for gsubing each time, and if so, where to implement it? Or should I use delocalize or globalize. I tried delocalize, but I get errors because I use specific activerecord, namely 4.0.4. Since I don't need any other localization (for now) globalize might be too much?

Kind regards, Seba


Solution

  • I wouldn't use gsub

    Try this:

    to_s.tr!(',','.').to_i
    

    You can add it to a method and invoke it before_save or a class method and call in where you need it.