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

On Nov 20, 9:31 pm, Peter M. [email protected]
wrote:

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

gene tani wrote:

String#tr and tr!, assuming that nobody enters EUropean format
numbers,

class String - RDoc Documentation

Thanks 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?

On Nov 21, 2007, at 2:56 AM, Peter M. 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”

On Nov 21, 2007, at 2:56 AM, Peter M. 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”

On Nov 20, 2007, at 9:31 PM, Peter M. 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)

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 :slight_smile:

Peter

On Nov 21, 2007, at 4:51 PM, Peter M. 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 :slight_smile:

You can get the entered value directly via params[:event][:amount]

So for instance,
3. payment.amount = params[:event][:amount].gsub(/[^\d.]/, ‘’)

George B. 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?

On Nov 21, 2007, at 6:37 PM, Peter M. 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.