# Need Help, getting Big Decimal error on a simple calculation

**URL:** <https://www.ruby-forum.com/t/need-help-getting-big-decimal-error-on-a-simple-calculation/124649>\
**Category:** Rails\
**Created:** [December 31, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/need-help-getting-big-decimal-error-on-a-simple-calculation/124649 "2007-12-31T16:31:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jooma2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jooma2](https://www.ruby-forum.com/u/jooma2)\
**Post date:** [December 31, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/need-help-getting-big-decimal-error-on-a-simple-calculation/124649/1 "2007-12-31T16:31:40Z")

</div>

Ok this is driving me nuts.

Here’s my issue in it’s most basic form.

I have a table with a column called amount that was set to :decimal,  
:precision =\> 8, :scale =\> 2

In my controller I try to do something like this.

id = 1  
@ticket = Ticket.find(:first, :conditions =\> [‘id=?’,id])  
@ticket.total\_amount += params[:amount]  
@ticket.direct\_amount += params[:amount]  
@ticket.save

This returns an error saying that it ‘can’t copy BigDecimal’

If I try to user .to\_f on my total\_amount and direct\_amount and  
params[:amount] I just end up getting an error about float allocation.

Any ideas? What info can I provide to make this more helpful?

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![jooma2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jooma2](https://www.ruby-forum.com/u/jooma2)\
**Post date:** [December 31, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/need-help-getting-big-decimal-error-on-a-simple-calculation/124649/2 "2007-12-31T16:46:58Z")

</div>

On Mon, Dec 31, 2007 at 04:31:40PM +0100, Jaime JB wrote:

> id = 1  
> Any ideas? What info can I provide to make this more helpful?  
> The actual exception/error messages would be helpful. I’m assuming that  
> the  
> Ticket model’s total\_amount and direct\_amount database fields are  
> decimals  
> and therefore ActiveRecord treats them as BigDecimal objects as well as  
> this amount column you mentioned (which table is it in? You don’t say).  
> I  
> would expect you to get exceptions like:

TypeError: String can’t be coerced into BigDecimal

or

TypeError: nil can’t be coerced into BigDecimal

The values in the params hash are always nil, a string, a hash, or an  
array. Since params comes from the parameters sent as text via HTTP by  
the  
web browser, Rails has no way of knowing that params[:amount] should be  
a  
number.

Now, I don’t see how you would be getting a “can’t copy BigDecimal”  
error  
given the code you show. You should examine the stacktrace and make sure  
that the exception is occurring where you think it is. That exception  
will  
happen if you try to #dup or #clone a BigDecimal, but I don’t see you  
doing  
that.

> Thanks for any help.  
> –Greg
