[LRUG] Prices and Precision

Laurie Young laurie at wildfalcon.com
Thu Nov 22 03:55:16 PST 2007


I would always use an integer type for money (ie the number of pence) and
never a float. The reason being that the rules for rounding monetary values
do not coincide with the rules for rounding floating point numbers. The
classic example is what happens when you divide £1000 by 3. You get £333.33.
However if you then multiply that by 3 you get £999.99, a penny has gone
missing, which is not good! The correct answer for dividing £1000 by 3 is
£333.33 + £333.33 + £333.34.

If you want more details on the best way to model money, look at Martin
Fowler's analysis of the subject:
http://martinfowler.com/ap2/quantity.html (scroll down the the Money
section)

On 18/11/2007, Dan Webb <dan at danwebb.net> wrote:
>
> Ive been working on and off on a money plugin for Rails that's pretty
> well developed now.  It's got currency conversion, reliable maths and
> support for active record money fields.  Its not really public but if
> you want to have a look then:
>
> http://svn.danwebb.net/external/rails/plugins/acts_as_money/
>
> I use it in nearly all of my projects.
>
> On 11/18/07, Ed Davey <ed at veryreal.co.uk> wrote:
> > >
> > > What's the best way to store prices in a Ruby (Rails) app?  What's the
> > > best SQL type for mySQL?
> > >
> >
> > I've been storing prices in pence as integers and using Brad Folkens
> > Model Formatter:
> >
> > http://brad.folkens.com/formatting-data-in-rails-models
> >
> > to display and enter prices in £2.99 format.
> >
> > This is working well for me so far...
> >
> > ED
> >
> > _______________________________________________
> > Chat mailing list
> > Chat at lists.lrug.org
> > http://lists.lrug.org/listinfo.cgi/chat-lrug.org
> >
>
>
> --
> Dan Webb
> http://www.danwebb.net
>
> aim: danwrong123
> skype: danwrong
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20071122/7972167d/attachment-0003.html>


More information about the Chat mailing list