ruby - Stripping commas from Integers or decimals in rails -


is there gsub equivalent integers or decimals? should gsub work integers? i'm trying enter decimal ruby form , user able use commas. example, want user able enter 1,000.99.

i've tried using

before_save :strip_commas  def strip_commas     self.number = self.number.gsub(",", "")     end 

but following error "undefined method `gsub' 8:fixnum" "8" replaced whatever number user enters.

if field fixnum, never have commas, rails have convert user input number in order store there.

however, calling to_i on input string, not want. overriding normal setter like

def price=(num)   num.gsub!(',','') if num.is_a?(string)   self[:price] = num.to_i end 

not tested, resembling should work...

you need @ commas while input still string.

edit: noted in comments, if want accept decimals, , create not integer, need different conversion string.to_i. also, different countries have different conventions numeric punctuation, isn't complete solution.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -