From: Charles Mills Date: 2004-10-29T01:52:09+09:00 Subject: Re: Rounding error, (100.0 * 9.95).to_i == 994 On Oct 28, 2004, at 9:12 AM, trans. (T. Onoma) wrote: > > How 'bout a good Money class ? > IMHO a good Money class would be priceless :) There was an interesting article in Dr. Dobbs about a java money class a while back. Below is the extract (registration required for full article). Anyway if you do a lot of monetary calculations using floating point you can get some pretty hefty round off error... as you guys have been discussing. -Charlie www.ddj.com/documents/ddj0405i/ ------------------------------------------------------------------------ --------- Java & Monetary Data Dr. Dobb's Journal May 2004 A standalone class to tackle the problem By John N. Armstrong John is a senior partner and owner of Objective Logic L.L.P. in McKinney, Texas, a software consulting firm specializing in Java-centric architectures and solutions. He can be reached at john@objectivelogic.com. Decimal Data Java and J2EE have become ubiquitous as a platform of choice for creating robust enterprise e-commerce applications. Although these kinds of applications typically involve operations on monetary data (computing unit and extended prices, tax amounts, shipping costs, and so on), Java does not provide suitable mechanisms for dealing with this type of data. Consequently, programmers are often faced with the problem of how to reliably represent, store, and manipulate monetary data. Obviously, when dealing with money, the importance of "getting it right" cannot be overstated. For these and similar reasons, an e-commerce application architecture should include a consistent mechanism for dealing with monetary data. In this article, I'll examine some of the more common techniques for dealing with monetary data, and show why these techniques are less than optimal. I also present Money, a standalone Java class that addresses the problem of dealing with monetary data. ------------------------------------------------------------------------ -------------