# \[Propose\] decimal columns propagates scale into BigDecimal

**URL:** <https://www.ruby-forum.com/t/propose-decimal-columns-propagates-scale-into-bigdecimal/84244>\
**Category:** Rails\
**Created:** [February 2, 2007, 5:53pm UTC](https://www.ruby-forum.com/t/propose-decimal-columns-propagates-scale-into-bigdecimal/84244 "2007-02-02T17:53:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ricardo\_PSSSSrez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9fc348/32.png) [@Ricardo\_PSSSSrez](https://www.ruby-forum.com/u/Ricardo_PSSSSrez)\
**Post date:** [February 2, 2007, 5:53pm UTC](https://www.ruby-forum.com/t/propose-decimal-columns-propagates-scale-into-bigdecimal/84244/1 "2007-02-02T17:53:45Z")

</div>

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):

[http://dev.rubyonrails.org/ticket/7402](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.

---

<div class="post-metadata">

**Author:** ![Ricardo\_PSSSSrez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9fc348/32.png) [@Ricardo\_PSSSSrez](https://www.ruby-forum.com/u/Ricardo_PSSSSrez)\
**Post date:** [April 17, 2009, 8:54am UTC](https://www.ruby-forum.com/t/propose-decimal-columns-propagates-scale-into-bigdecimal/84244/2 "2009-04-17T08:54:17Z")

</div>

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.
