From: "Brian Schröder" Date: 2005-04-20T07:55:17+09:00 Subject: Re: String<->float conversion headaches On 20/04/05, Michael Campbell wrote: > On 4/19/05, Brian Schr�der wrote: > > On 19/04/05, pmak@aaanime.net wrote: > > > The corresponding column in the MySQL database is of type DECIMAL(7,2), > > > but the corresponding Ruby structure came out as a String instead of a > > > decimal type. I haven't updated my database driver in a few years, > > > though; perhaps I'll look into that. Thanks. > > > > > > > > > > DECIMAL(7.2) should not be converted to a float value, as floats can't > > represent all instances of DECIMAL(7.2). For example 0.1 can not be > > represented as a float. > > > > irb(main):010:0> "%.18f" % 0.1 > > => "0.100000000000000006" > > In general, using non BCD type number encoding systems, *No* real > number that isn't a summation of powers of 2 can be represented by a > float. > > By your logic then, does this mean that they just shouldn't be used > for any "DECIMAL(x, y)" (y > 0) field in a database? > > That depends on what you are using the numbers for. If it is for simulation of something the float resolutions are good, but in this special example decimal(7,2) hinted a monetary value to me, and there it may be bad if you sum up rounding errors. best regards, Brian -- http://ruby.brian-schroeder.de/ multilingual _non rails_ ruby based vocabulary trainer: http://www.vocabulaire.org/ | http://www.gloser.org/ | http://www.vokabeln.net/