Re: Representing money
Greg Navis <[email protected]>
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <CAA6WWt8qp3Z-JoFSdGqJAhTvA4ajWGiRROgsrryvD7x0PC9fHg@mail.gmail.com> |
Use integers and represent $1.23 as 123. If you need to specify the currency then you may need a custom class (or a gem) but that problem is orthogonal to representing the numerical part. Decimals won't save you because if you send "1.333" to an third-party system then it can either raise an error or truncate to "1.33". You need to keep in mind that money arithmetic is different from normal arithmetic. Say you want to divide $10 into 3 installments. If you do rationals then 10 / 3 is 3 x 3.(3) which likely will get truncated to 3.33 and you'll end up with 9.99 in total. Same for increasing/decreasing amounts by a given percentage. For example, if you want to give a 10% discount off $9.99 then the exact result is $8.991. Best regards Greg Navis Unsubscribe: <mailto:[email protected]?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>