Forum: Ruby on Rails Storing "money" in databases

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and 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)

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?

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

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!


Robert Dempsey
Project Director
Atlantic Dominion Solutions, LLC


Visit our blog for all the latest at
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://

Benjamin Curtis -- Collaborative test case management -- 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

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

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://

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

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

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:

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 <> wrote:
> regular-decimal type.  I guess the BigDecimal type has problem.  I'd guess the problem 
> 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.

This topic is locked and can not be replied to.