This will be completely technical post. I want just to leave somewhere a solution for a problem which I had to solve some time ago. I hope that there is some gem to do that. But the only one I found, which could fit my needs, is Delocalize, but it do some stuff on my models - I wanted to keep my models intact.
The problem is, that rails interpret numbers in format “1.23”, but in many countries we use “1,23”, and we have only ‘,’ on numpad, so if users have to type a lot of numbers in forms (usecase in my application), it’s obviously a pain.
In first gist, we have usage example in controller. In the second, an implementation of these method. It can probably be done in cleaner & easier way. Third file shows an implementation of decimal_number_field, which I use in my views.
The other thing I want to mention is that I have to always truncate my decimals when saving, instead of rounding them if they are longer, than defined “scale”. I found out that “The precise behavior is operating system-specific, but generally the effect is truncation to the permissible number of digits.” (from Stack Overflow), but unfortunately, on my system default behaviour was rounding a number, so I decided to create an observer.
And that’s all. It’s my first english post and I hope it isn’t as horrible as I think it is.