# Dealing with Money Calculations

**URL:** <https://www.ruby-forum.com/t/dealing-with-money-calculations/138689>\
**Category:** Rails\
**Created:** [May 26, 2008, 6:30pm UTC](https://www.ruby-forum.com/t/dealing-with-money-calculations/138689 "2008-05-26T18:30:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [May 26, 2008, 6:30pm UTC](https://www.ruby-forum.com/t/dealing-with-money-calculations/138689/1 "2008-05-26T18:30:23Z")

</div>

Ok so this is probably the 100th time someone has brought this up, but  
searching the group brings up all the spam emails, and nothing of  
value… so I’m going to try again.

I’m dealing with a LOT of numbers in my application, and a lot of  
calculations… and I’m starting to run into problems when I’m adding  
two percentages of a number together, due to rounding, and I’m off a  
cent or two here or there…

I found this plugin;

[http://agilewebdevelopment.com/plugins/rails\_money](http://agilewebdevelopment.com/plugins/rails_money)

and on the surface it looks good, but I was wondering what other  
people use to solve this, and any input on this would be greatly  
appreciated.

Thanks in advanced  
Randal

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [May 26, 2008, 8:49pm UTC](https://www.ruby-forum.com/t/dealing-with-money-calculations/138689/2 "2008-05-26T20:49:33Z")

</div>

Plain ol’ decimal datatype works good as well.

> **[ActiveRecord::ConnectionAdapters::TableDefinition](https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#M001222has)**
>
> Represents the schema of an SQL table in an abstract way. This class provides methods for manipulating the schema representation.

a lot of information about specifying the precision on the decimal  
field.

RSL

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [May 26, 2008, 8:57pm UTC](https://www.ruby-forum.com/t/dealing-with-money-calculations/138689/3 "2008-05-26T20:57:50Z")

</div>

The problems arise when you’re doing calculations with those  
numbers…example… if I want 2% of a number it might be  
123.3352222 which is 123.34 in money talk… and say I have that  
twice then it’s 123.335111111 + 123.3351111 = 246.670111111 or rounded  
up to 246.67. However in “money calc” it’s actialy 246.68 because  
123.335 is 123.34 which is then added together twice to get you 123.34

- 123.34.

Obviously that’s a messy examplee, but it should illustrate the  
point… the decimal datatype is only rounded when it’s put into the  
database… a lot of my displayed calcs are done using methods IE  
@invoice.show\_total might be a method that takes the QTY \* COST of  
each lines, does any Tax Calcs and displays the total… I don’t need  
to store this number. ( again… rough example… but you should get  
the idea )

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [May 26, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/dealing-with-money-calculations/138689/4 "2008-05-26T22:23:18Z")

</div>

Something is defiantely up then… why is there a need for this  
plugin then?

[http://agilewebdevelopment.com/plugins/rails\_money](http://agilewebdevelopment.com/plugins/rails_money)

I’m losing a lot of pennies here and there and it’s all on  
%calculations…

Any ideas?

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [May 26, 2008, 10:49pm UTC](https://www.ruby-forum.com/t/dealing-with-money-calculations/138689/5 "2008-05-26T22:49:50Z")

</div>

sw0rdfish wrote the following on 26.05.2008 22:22 :

> Something is defiantely up then… why is there a need for this  
> plugin then?
> 
> [http://agilewebdevelopment.com/plugins/rails\_money](http://agilewebdevelopment.com/plugins/rails_money)
> 
> I’m losing a lot of pennies here and there and it’s all on  
> %calculations…
> 
> Any ideas?

Probably because temporary results of your calculations get stored in  
database. BigDecimal will keep exact numbers, the database will store  
float approximations.  
The plugin you refer to ensures that when you handle money values in  
Database they keep the exact same value than Ruby ones, assuming your  
precision is always at best 1/100 in the currency you are working with.  
So you can dump/load values in database in the middle of your  
calculations without affecting the results (always with the limitation  
that you can’t use more precision than 1/100).

Lionel

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [May 26, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/dealing-with-money-calculations/138689/6 "2008-05-26T21:50:52Z")

</div>

> The problems arise when you’re doing calculations with those  
> numbers…example… if I want 2% of a number it might be  
> 123.3352222 which is 123.34 in money talk… and say I have that  
> twice then it’s 123.335111111 + 123.3351111 = 246.670111111 or rounded  
> up to 246.67. However in “money calc” it’s actialy 246.68 because  
> 123.335 is 123.34 which is then added together twice to get you 123.34
> 
> - 123.34.

Decimal columns in the database get typecast to the Ruby BigDecimal  
type, so your calculations should be fine.

Cheers,  
Jordan
