开发者

gsub within before_validation method is zeroing out my value

开发者 https://www.devze.com 2023-01-06 18:41 出处:网络
I\'m trying to remove dollar signs and commas from my form input (for example, $1,000.00 => 1000.00) I have the following following line in my before_validation method in my model:

I'm trying to remove dollar signs and commas from my form input (for example, $1,000.00 => 1000.00)

I have the following following line in my before_validation method in my model:

self.parents_mortgage = self.parents_mortgage.to_s.g开发者_如何学JAVAsub!('$,','').to_i

This is causing any number to be put through to zero out. Is there something wrong with my syntax?


use tr:

self.parents_mortgage = self.parents_mortgage.to_s.tr!('$,','').to_i


self.parents_mortgage = self.parents_mortgage.to_s.gsub!('$','').gsub!(',','').to_i


self.parents_mortgage = self.parents_mortgage.to_s.gsub('/[\$,]/','').to_i

Because yes there was a problem with your syntax. Your regex needs the / wrapping, it needs to indicate that $ and , are a character group (by surrounding with []) and you need to escape the $

0

精彩评论

暂无评论...
验证码 换一张
取 消