[LRUG] Prices and Precision
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
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:
> 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
> aim: danwrong123
> skype: danwrong
> Chat mailing list
> Chat at lists.lrug.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Chat