[snippet] Cleaning up currency strings

Posted by paul Wed, 18 Jan 2006 06:46:00 GMT

I was working on some data that I needed to import into mysql for a new rails project I’ve started.

Some of the fields are currency strings, eg £4,000.00. Now initially I was tempted to just bung them into the table as varchars but there is a good chance that at some point down the line I’m going to want to ‘do maths’ with these numbers so I really had to convert them to floats.

In the end I used this little bit of code which worked a treat:

return amount.gsub(/[£,]/,’‘)

So what this does is matches the characters £ and , and substitutes them with blank. Works a treat.

Comments

Leave a response

Comments