On Friday, April 13, 2012 4:08:31 PM UTC-7, Ollie Frolovs wrote: > > Hello everyone > > Here I am again with my little study project - > > I have some BigDecimal fields in my table ServiceProvisions, like > > *BigDecimal :weekly_charge, :size => [18,4]* > > The values are returned as BigDecimals: > > *irb(main):015:0> DB[:service_provisions].first[:weekly_charge] > * > *=> #<BigDecimal:1732380,'0.22E0',9(18)>* > > and then I add dataset aggregation methods to the model for every decimal > field that I have: > > *ServiceProvision.columns.each do |column_name|* > * if ServiceProvision.db_schema[column_name][:type] == :decimal* > * ServiceProvision.def_dataset_method(column_name) do* > * sum(column_name)* > * end* > * end* > *end* > * > * > The aggregation function sum however returns a Float: > > *irb(main):019:0> DB[:service_provisions].sum(:weekly_charge).class* > *=> Float* >
> The question is does Sequel add them all up as BigDecimal values and *then > * converts to Float or does it convert *first* and then adds them up? > > Most likely, Sequel isn't doing any conversion at all, the conversion is > being done in your database. You didn't specify which database you are > using. Are you sure the database's sum function returns a numeric value > and not a double precision value? For example, SQLite doesn't support > decimal values, and it's sum function returns a double precision value. > > Jeremy > On Friday, April 13, 2012 4:08:31 PM UTC-7, Ollie Frolovs wrote: > > Hello everyone > > Here I am again with my little study project - > > I have some BigDecimal fields in my table ServiceProvisions, like > > *BigDecimal :weekly_charge, :size => [18,4]* > > The values are returned as BigDecimals: > > *irb(main):015:0> DB[:service_provisions].first[:weekly_charge] > * > *=> #<BigDecimal:1732380,'0.22E0',9(18)>* > > and then I add dataset aggregation methods to the model for every decimal > field that I have: > > *ServiceProvision.columns.each do |column_name|* > * if ServiceProvision.db_schema[column_name][:type] == :decimal* > * ServiceProvision.def_dataset_method(column_name) do* > * sum(column_name)* > * end* > * end* > *end* > * > * > The aggregation function sum however returns a Float: > > *irb(main):019:0> DB[:service_provisions].sum(:weekly_charge).class* > *=> Float* > > The question is does Sequel add them all up as BigDecimal values and *then > * converts to Float or does it convert *first* and then adds them up? > Most likely, Sequel isn't doing any conversion at all, the conversion is being done in your database. You didn't specify which database you are using. Are you sure the database's sum function returns a decimal value and not a double precision value? For example, SQLite doesn't support decimal values, and its sum function returns a double precision value (or an integer value if all column values are integers). Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/sequel-talk/-/kVeEdO3qpOsJ. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sequel-talk?hl=en.
