Forum: Ruby on Rails Storing "money" in databases

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5e518814b76962fdd1ad0e74dfac5ea7?d=identicon&s=25 Martin Gamsjaeger (Guest)
on 2006-06-12 12:01
(Received via mailing list)
Hiall,

I'm wondering what's common practice when dealing with money values
stored in databases. I like the idea of storing all the values as
integers, i.e. all the values in eurocents not euros. Then I need to
multiply all values the users enter by 100 before storing them in the
database. Now the question is, what's the most efficient and dryest
way to do this? I'm not sure if I want to override every accessor
activerecord generates for me ...

Any ideas?

cheers
Martin
Cf17040cecb2618506f5ca4c14957957?d=identicon&s=25 Robert Dempsey (flatekmonkey)
on 2006-06-12 13:40
(Received via mailing list)
Martin,

Thus far (at least for dollars) we have been creating a decimal column
and using that to store monetary values. I am not sure how the euro
works but that works for us. Good luck!

--
Sincerely,

Robert Dempsey
Project Director
Atlantic Dominion Solutions, LLC

321.356.6855
http://www.techcfl.com

Visit our blog for all the latest at http://www.techcfl.com/blog
Db303dc84d03a992b33cd3ac978f89ae?d=identicon&s=25 Benjamin Curtis (Guest)
on 2006-06-12 15:03
(Received via mailing list)
On Jun 12, 2006, at 2:59 AM, Martin Gamsjaeger wrote:

> Any ideas?
>
> cheers
> Martin

Check out the Dollars and Cents plugin: http://
agilewebdevelopment.com/plugins/dollars_and_cents

--
Benjamin Curtis
http://www.bencurtis.com/
http://www.tesly.com/ -- Collaborative test case management
http://www.agilewebdevelopment.com/ -- Resources for the Rails community
23172b6630dc631a134c9bad2fec2a39?d=identicon&s=25 Chris (Guest)
on 2006-06-12 15:07
Given issues with representing floats accurately in binary I was under
the impression that any app that requires accuracy would stick with
ints.

The only time you really need to convert to the regular format would be
displaying, yes?


Converting the inputs sounds like a good place for a Insert/update
trigger.

Cheers
Chris
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-06-12 19:24
(Received via mailing list)
On Jun 12, 2006, at 6:00 AM, Benjamin Curtis wrote:

>> activerecord generates for me ...
>
>
> Check out the Dollars and Cents plugin: http://
> agilewebdevelopment.com/plugins/dollars_and_cents

Interesting. Hadn't heard of that one.

I *have* used the Money gem however, and like it quite a bit.

gem install money

It stores in cents, and has currency conversion and output formatting
as well.

You can use ActiveRecord composition to make it play nicely with the DB.

--
-- Tom Mornini
E6dd9cedee99f9d02e5f87d80ee0e681?d=identicon&s=25 Warren Seltzer (Guest)
on 2006-06-12 19:57
(Received via mailing list)
The common practice is a decimal field with places to the right of the
decimal point.
Usually two places.  Standard SQL has a decimal data type. There is a
reason for this.
They would not have gone to all the trouble if it were a minor point.
Numerous machine
hardware architectures have a decimal type in addition to float and
integer.

Do you dare to test floating point numbers for equality?

While the Ruby library has a BigDecimal data type it has no "Ordinary"
decimal type.  Ruby
has integers and floats.  So Rails uses floats for money.  This is not
the right answer.
This is the biggest hole I know of in Ruby and Rails.  Ruby apparently
needs a better
regular-decimal type.  I guess the BigDecimal type has problem.  I'd
guess the problem is
performance.  Another is formatting the output of BigDecimal numbers and
rounding.

There are some good articles around somewhere that describe in detail
the reasons for the
innovation of fixed-decimal numbers and why floats are not adequate for
financial data
processing.  Mostly to do with handling of rounding and the fuzziness of
comparison. See:

http://msdn2.microsoft.com/en-us/library/ms187912.aspx


Warren Seltzer
5233478c51a92b6a1a5c970cbf3a42f3?d=identicon&s=25 Isak Hansen (Guest)
on 2006-06-13 17:17
(Received via mailing list)
On 6/12/06, Warren Seltzer <warrens@actcom.net.il> wrote:
> regular-decimal type.  I guess the BigDecimal type has problem.  I'd guess the problem 
is
> performance.  Another is formatting the output of BigDecimal numbers and rounding.

I think choosing float over bigdecimal was more of an
oversight/shortcut that's never been fixed.

I never tested BigDecimal performance, but it can't be that bad, and
certainly not horrid enough to make me suddenly stop caring about the
accuracy I had in mind while designing my db.

Isak
This topic is locked and can not be replied to.