« Rails Guidebook | Main | Pluvo »

June 29, 2006

Prices in Cents

In the second edition of AWDwR, I changed the Depot application to store money in integral cents, rather than floating point dollars and cents. Some folks pushed back on this, saying it overly complicated the application.

This morning, while working on some code to migrate some legacy data, I bumped into a problem that boiled down to the following (on my PPC—your results may differ)

  dave[store/migrate 9:00:57] irb
  irb(main):001:0> Integer(77.85 * 100.0)
  => 7784

Obviously it’s possible to do rounding (although even that’s trickier than you might first code if you have to handle negative numbers).

I still feel that Rails should convert decimal(x,y) columns into BigDecimal format in Ruby, giving us scaled, exact, representations. Until that day, stick with integers for amounts that you need to be exact.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/t/trackback/2226312/7670401

Listed below are links to weblogs that reference Prices in Cents:

Comments

Post a comment

If you have a TypeKey or TypePad account, please Sign In

Now in Beta

  • Programming Ruby, 3rd Edition
    Third Edition, Covering Ruby 1.9, now in beta
My Photo

Pragmatic Stuff

Photos

  • www.flickr.com
    This is a Flickr badge showing public photos from pragdave tagged with pragdave_badge. Make your own badge here.

Site Search

  • Google Search

    The web
    PragDave