[Propose] decimal columns propagates scale into BigDecimal

decimal(x,y) columns in Rails 1.2 are mapped to BigDecimal object. They
works well, but it could be better.

For example, one column:

price decimal(5,2)

for an “articles” table is mapped to a BigDecimal object, but that
object doesn’t “remember” the corresponding column’s :scale.

Therefore,

article = Articles.find(1) article.price # this object is a BigDecimal
one, but it doesn’t knows how many fractional digits must contain

So “article.price.to_s” will not “render” the correct fractional digits
based of the column’s scale.

For example, if we save the 5.20 value into the column, then the
expression “article.price.to_s” will return “5.2”.

In that way, if we save the 5 value, the expression “article.price.to_s”
will return “5.0”.

If the BigDecimal object could “remember” how many decimals it must to
show, then the to_s method could always give you more appropriate
results. In the case of 5.20, to_s will returns “5.20”; in the case of
5, to_s will returns “5.00”. In the same way, if the column were
decimal(5), when you save 5.20 or 5 values, to_s will always returns
“5”.

The patch I developed tries to solves the problem. With this patch, a
BigDecimal object have a scale attribute, so the to_s method can know
how to print the decimal correctly.

The patch touches files from ActiveRecord and ActiveSupport. The
ActiveRecord part puts the scale into the BigDecimal object, and the
ActiveSupport part prints the BigDecimal object according to the scale
within in.

I created a ticket into dev.rubyonrails.org:

http://dev.rubyonrails.org/ticket/7402

In that ticket you can get the patch (in diff format).

The ticket was closed because my implementation has several issues. I do
a call here to all the Rails experts in order to get a better
implementation to get the scale be propagated from the decimal columns
into the BigDecimal objects.

Anybody have an idea to do this?

Thanks a lot.

I’m using Rails 2.2.2 and I’m having the same problem here.

d=BigDecimal(‘10’,2)
=> #BigDecimal:24647c0,‘0.1E2’,4(8)

(d/3).to_s
=> “3.333333333333333333333333”

(d/3).to_f.round_with_precision(2)
=> 3.33

Do I have to convert always the BigDecimals to float in every math
operation and then use the round_with_precision ?

This has been solved in the 2.3 ?

patching the Rails is an option but …

Any better ideas on how to accomplish that ?

thanks …

r.