1

I have a field -

:Revenue

and it should accept values like 10,000.00, but if I input such value it stores 10 into database instead of 10000.00

What should I do to strip of commas before I save?

I've tried to find a few solutions online but wasn't able to implement them as I found them incomplete. If someone could help me I would really appreciate it.

**The problem now I am facing is that as soon as I enter the value rails converts string in to float value before it can run the gsub function, like if I enter 50,000.00 its converting into float 50.0 before calling the gsub, is there any way to over the to_f method which rails is calling on the string.

lurker
  • 56,987
  • 9
  • 69
  • 103
Hrishikesh Sardar
  • 2,907
  • 4
  • 21
  • 33
  • 3
    What were those `incomplete` solutions? – devnull Jun 27 '13 at 16:34
  • See if this helps. I think it is exactly what you're trying to do and is more complete than the answer I had given: http://stackoverflow.com/questions/6541209/decimals-and-commas-when-entering-a-number-into-a-ruby-on-rails-form Go to the answer that has **5** points. – lurker Jun 27 '13 at 18:25

3 Answers3

13

Removing commas is pretty simple:

value.gsub(/,/, '').to_f

Keep in mind that European formatting often uses comma as the decimal value separator so your results would be off by a factor of 100 if processing those sorts of numbers.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 2
    Unless you can guarantee that there will be at least one comma, you will not want to use the bang method, otherwise your value will go from 10,000.00 to 0. – vgoff Jun 27 '13 at 16:57
  • "NoMethodError Exception: undefined method `gsub' for 50.0:Float" if i enter something like 50,000.00 – Hrishikesh Sardar Jun 27 '13 at 17:19
  • @HrishikeshSardar If you are trying this on a float, you've already done the conversion and it's too late. Maybe you need to process the `params` before you link them up with the model? – tadman Jun 27 '13 at 17:44
  • do u know how can i process the params before i link it up with the database? – Hrishikesh Sardar Jun 27 '13 at 17:47
  • In your controller if you have one. You haven't provided any code so I can only guess. – tadman Jun 27 '13 at 17:48
4

You can take a String#delete.

"10,000,000.00".delete(',').to_f 
# => 10000000.0
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
  • 1
    This has the same problem as gsub! if you can't guarantee there will be a comma, you will get 0 rather than 10 million. – vgoff Jun 27 '13 at 18:30
  • the problem is as soon as enter 10,000.0 and click on submit it converts it into a float so it becomes 10, i want to use gsub or delete in this case before that happens – Hrishikesh Sardar Jun 27 '13 at 18:33
  • @vgoff changed the post.. Thanks for your comment. – Arup Rakshit Jun 27 '13 at 18:33
1

I found the solution after looking at few places and combining few solutions, since I had to use gsub before the linking to model has to be done. so I created the method in my controller and called it before create and update action. and wrote the following code in the method

params[:record][:Revenue] = params[:record][:Revenue].gsub(/,/,"")
Hrishikesh Sardar
  • 2,907
  • 4
  • 21
  • 33