How to strip commas from float input?

前端 未结 3 991
悲哀的现实
悲哀的现实 2021-01-25 04:12

I have a field -

:Revenue

and it should accept values like 10,000.00, but if I input such value it stores 10 into dat

相关标签:
3条回答
  • 2021-01-25 04:52

    You can take a String#delete.

    "10,000,000.00".delete(',').to_f 
    # => 10000000.0
    
    0 讨论(0)
  • 2021-01-25 05:02

    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(/,/,"")
    
    0 讨论(0)
  • 2021-01-25 05: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.

    0 讨论(0)
提交回复
热议问题