Peter Marks
2007-Nov-21 05:31 UTC
Best way to sanitize a decimal field (remove commas and $''s)
What''s the best way to convert a number that may contain dollar signs or commas to a plain decimal? It seems like there would be a helper for this, but I haven''t found one. I could just declare this data invalid upon entry and make the user re-enter the number, but the people using my software are older and don''t seem to be breaking their habit of entering in numbers with baggage. Any ideas? Thanks, Peter -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gene tani
2007-Nov-21 08:04 UTC
Re: Best way to sanitize a decimal field (remove commas and $''s)
On Nov 20, 9:31 pm, Peter Marks <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> What''s the best way to convert a number that may contain dollar signs or > commas to a plain decimal? It seems like there would be a helper for > this, but I haven''t found one. > > I could just declare this data invalid upon entry and make the user > re-enter the number, but the people using my software are older and > don''t seem to be breaking their habit of entering in numbers with > baggage. Any ideas? > > Thanks, > > Peter > -- > Posted viahttp://www.ruby-forum.com/.String#tr and tr!, assuming that nobody enters EUropean format numbers, http://www.ruby-doc.org/core/classes/String.html#M000845 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Peter Marks
2007-Nov-21 08:56 UTC
Re: Best way to sanitize a decimal field (remove commas and
gene tani wrote:> String#tr and tr!, assuming that nobody enters EUropean format > numbers, > > http://www.ruby-doc.org/core/classes/String.html#M000845Thanks for your response Gene. tr seems to work for removing letters and numbers: "100A".tr(''A'', '''') #=> "100" "1002".tr(''2'', '''') #=> "100" When I use a comma or dollar sign, however, it sets my value to zero: "1,000".tr('','', '''') #=> "0" "$1000".tr(''$'', '''') #=> "0" "$1,000".tr(''$,'', '''') #=> "0" Is this proper use of tr? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
George Bailey
2007-Nov-21 21:00 UTC
Re: Best way to sanitize a decimal field (remove commas and
On Nov 21, 2007, at 2:56 AM, Peter Marks wrote:> When I use a comma or dollar sign, however, it sets my value to zero: > > "1,000".tr('','', '''') #=> "0" > "$1000".tr(''$'', '''') #=> "0" > "$1,000".tr(''$,'', '''') #=> "0" > > Is this proper use of tr?If you want to ''translate'' to "", delete is more specific. >> "1,000,000".delete "," => "1000000" --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
George Bailey
2007-Nov-21 21:06 UTC
Re: Best way to sanitize a decimal field (remove commas and
On Nov 21, 2007, at 2:56 AM, Peter Marks wrote:> When I use a comma or dollar sign, however, it sets my value to zero: > > "1,000".tr('','', '''') #=> "0" > "$1000".tr(''$'', '''') #=> "0" > "$1,000".tr(''$,'', '''') #=> "0" > > Is this proper use of tr?To follow up, you can delete both at once: >> "$2,345".delete "$," => "2345" By the way, pasting in your first your example above I get this: "1,000".tr('','', '''') => "1000" --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Greg Willits
2007-Nov-21 21:17 UTC
Re: Best way to sanitize a decimal field (remove commas and $''s)
On Nov 20, 2007, at 9:31 PM, Peter Marks wrote:> What''s the best way to convert a number that may contain dollar > signs or > commas to a plain decimal?newNumber = number.gsub(/[^\d\.]/, '''') Or use gsub! All the other methods don''t account for stripping other garbage characters that people/exports might toss into strings. like () or - for negative nummbers. Take the approach of extracting only what you want, not removing what you don''t want. "Balance: $ -(123,345.098) extra junk".gsub(/[^\d\.]/, '''') -- gw (www.railsdev.ws) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Peter Marks
2007-Nov-21 22:51 UTC
Re: Best way to sanitize a decimal field (remove commas and
I''ve figured out what my problem is. All of these methods work (including the tr method which I previously thought didn''t). I''m just having trouble implementing it with my form. I use the ''enter_payment'' controller method to save the ''event'' object from my form. I added line 3 to sanitize ''amount'' before saving the event. 1 def enter_payment 2 payment = Event.new(params[:event]) 3 payment.amount = payment.amount.to_s.gsub(/[^\d\.]/, '''') 4 payment.save 5 end I''m guessing the the invalid ''amount'' field is being set to zero when I pass the event parameter to ''enter_payment''? If so, how can I sanitize before this? Thanks again for everyone''s help with this :) Peter -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
George Bailey
2007-Nov-21 23:19 UTC
Re: Best way to sanitize a decimal field (remove commas and
On Nov 21, 2007, at 4:51 PM, Peter Marks wrote:> 1 def enter_payment > 2 payment = Event.new(params[:event]) > 3 payment.amount = payment.amount.to_s.gsub(/[^\d\.]/, '''') > 4 payment.save > 5 end > > I''m guessing the the invalid ''amount'' field is being set to zero > when I > pass the event parameter to ''enter_payment''? If so, how can I sanitize > before this? Thanks again for everyone''s help with this :)You can get the entered value directly via params[:event][:amount] So for instance, 3. payment.amount = params[:event][:amount].gsub(/[^\d\.]/, '''') --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Peter Marks
2007-Nov-22 00:37 UTC
Re: Best way to sanitize a decimal field (remove commas and
George Bailey wrote:> You can get the entered value directly via params[:event][:amount] > > So for instance, > 3. payment.amount = params[:event][:amount].gsub(/[^\d\.]/, '''')That works great. Is there a way to incorporate this into a model validation so I don''t have to write it in every controller method? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
George Bailey
2007-Nov-22 01:13 UTC
Re: Best way to sanitize a decimal field (remove commas and
On Nov 21, 2007, at 6:37 PM, Peter Marks wrote:> That works great. Is there a way to incorporate this into a model > validation so I don''t have to write it in every controller method?Validation won''t have the entered info. You might be able to do it by overriding new, but that''s beyond me. Someone else would have to jump in here. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---