# Decimals are being converted to Bigdecimals!

**URL:** <https://www.ruby-forum.com/t/decimals-are-being-converted-to-bigdecimals/126567>\
**Category:** Rails\
**Created:** [January 19, 2008, 6:10pm UTC](https://www.ruby-forum.com/t/decimals-are-being-converted-to-bigdecimals/126567 "2008-01-19T18:10:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [January 19, 2008, 6:10pm UTC](https://www.ruby-forum.com/t/decimals-are-being-converted-to-bigdecimals/126567/1 "2008-01-19T18:10:27Z")

</div>

Hi all,

I’m working on a financial app, and have set up some columns in my  
tables as decimals with precision =\> 8 and scale =\> 2. My migration  
looks like:

```
change_column :lineitems, :price, :decimal, :precision =>

```

8, :scale =\> 2  
change\_column :payments, :amount, :decimal, :precision =\>  
8, :scale =\> 2

I’ve run the migration and restarted the server. I also looked at the  
Mysql tables to verify that the migration ran correctly, and the  
fields look like they’re set up right ( DECIMAL(8,2) ).

The problem is that when I access these columns in the console, they  
show up as Bigdecimal and it’s messing up my calculations. Here’s  
what it looks like from the rails console:

> > Lineitem.find(34).price  
> > =\> #BigDecimal:31e4c74,‘0.3E4’,4(12)
> 
> > Payment.find(23).amount  
> > =\> #BigDecimal:31b8480,‘0.4125E4’,4(12)

What’s going on here? How do I get Rails to recognize that these are  
decimal/currency amounts???

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [January 19, 2008, 9:09pm UTC](https://www.ruby-forum.com/t/decimals-are-being-converted-to-bigdecimals/126567/2 "2008-01-19T21:09:43Z")

</div>

On 1/19/08, Neal L [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 8, :scale =\> 2  
> =\> #BigDecimal:31e4c74,‘0.3E4’,4(12)
> 
> > > Payment.find(23).amount  
> > > =\> #BigDecimal:31b8480,‘0.4125E4’,4(12)
> 
> What’s going on here? How do I get Rails to recognize that these are  
> decimal/currency amounts???

BigDecimal is just the Ruby class which represents decimal fixed point  
numbers.

How is it “messing up your calculations?”

> > b = BigDecimal.new(“100”, 2)  
> > =\> #BigDecimal:13f4e6c,‘0.1E3’,4(8)  
> > b.to\_s  
> > =\> “100.0”  
> > b + 1  
> > =\> #BigDecimal:13f0b8c,‘0.101E3’,4(12)  
> > (b + 1).to\_s  
> > =\> “101.0”

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
