Dealing with Money Calculations...

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

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

Plain ol’ decimal datatype works good as well. http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#M001222 has a lot of information about specifying the precision on the decimal field.

RSL

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 )

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

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

http://agilewebdevelopment.com/plugins/rails_money

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

Any ideas?

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

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