Thanks for the suggestion Steve! Actually its financial numbers, so I do want decimal... Just need to know how precise they will be.
It depends on your use-case, but Rational might do what you're after? http://www.ruby-doc.org/core-2.1.0/Rational.html
--
You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rails-oceani...@googlegroups.com.
To post to this group, send email to rails-...@googlegroups.com.
Visit this group at http://groups.google.com/group/rails-oceania.
For more options, visit https://groups.google.com/groups/opt_out.
Probably not useful input, as I don't know your circumstances, but if you care about precision and it's financial, I'd think seriously about using integers (assuming of course that you haven't already). You get control of exactly where truncation occurs and in what direction.
It sounds like you've chosen a base unit, so you could work from there. At least you won't always be worried that somehow you've accidentally used floating point.
The money gem is definitely not what you are after as it rounds way too much. It may compute with integer computation (I havnt looked) however it stores 2 decimal places and seems to round between each computation.
This unfortunately makes it unusable for any money application where division or multiplication is needed. It is possibly useful to help translate between currencies.. however even then a higher precision would probably be in orderIf that doesn't sway you try (19.957).to_money on the console :)
--
This is possibly getting quite off track, but the money gem's lack of understanding of the details of different currencies is why I've always ended up rolling my own. Unfortunately I never actually get around to releasing it as a gem...
I also haven't looked at it in a while, but from what I remember the money gem's not afraid of the odd silent floating point calculation as well; which more-or-less rules it out for me.
However, there's nothing wrong with rounding/truncating between operations assuming it has the precision you need. It's quite possibly more correct.
The trick is to know when the rounding is occurring and minimise it. Or apply the appropriate legislated rules, if there are any. If you're relying on excess precision in the middle of an algorithm, you're still rounding, just not as obviously.
Anyway, let's hope Chris's original question gets answered; there are at least a few directions to go in this thread.
--
big_money looks okay, I've had a look over that one in the past. It
uses BigDecimal under the hood though, and comes with a fair bit of
baggage.