Re: Representing money

Walter Lee Davis <[email protected]>
Newsgroups gmane.comp.lang.ruby.general
Message-ID <[email protected]>
> On Jan 8, 2019, at 10:25 AM, Andy Jones <[email protected]> wrote:
> 
> Re: “Behaviour of round changed?”.  
> 
> Pardon my curiosity -- what do you use to represent money in your code?  It seems to me that the choices are between:
> 
> 1)      BigDecimal
> 
> 2)      Rational
> 
> 3)      A custom class
> 
> To my way of thinking, if you are multi-currency, you almost certainly want #3; but otherwise it’s down to personal preference?  I plumped for BigDecimal, but I wonder if it would have been easier on me if I had gone for Rational instead?
> 
>  

I use integer "cents" and the Money gem to deal with the localization details. This works perfectly in a multi-currency Web store. I only ever store cents, and convert all presentations on the fly into whatever passes for dollars and cents with a decorator method wrapped around Money:

...
Money.new(price_in_cents, currency).format
...

Walter

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.