From: Robert Klemme Date: 2005-04-18T21:49:36+09:00 Subject: Re: String<->float conversion headaches schrieb im Newsbeitrag news:1113823712.759455.162390@o13g2000cwo.googlegroups.com... > I'm programming an application that saves its data in a MySQL > database. I frequently run into headaches where I get errors like: > > undefined method `-' for "9000.00":String (NameError) > > because I typed > > source['salary'] -= encumbered['salary'] > > where source[] and encumbered[] were instantiated from > MysqlResult#fetch_hash, and are thus strings instead of integers or > floats. > > I write this sort of thing all the time in Perl and don't have trouble > because of its automatic type conversion. What would be the best way > to do this in Ruby? Right now I'm writing: > > source['salary'] = source['salary'].to_f - > encumbered['salary'].to_f > > everywhere and it feels quite tedious. One option is to change table data types to Float. DBI should then return proper Ruby instances (i.e. Floats) for them. This is also more efficient from a db storage point of view than having those numbers as strings in there. Kind regards robert