Once and for all: Do not use double for money
The discussion on how to represent money in applications pops up regularly . Funny lots of people still use double to represent money. During my interviews some people use double for money. Once and for all: Do not use double. Double cannot represent all amounts of money and it has rounding problems. Use a Money class. Represent the amount with a BigDecimal in cents and use BigDecimal.ROUND_HALF_EVEN as your rounding method. Use a distribute algorithm to distribute money to severel slots without losing cents.
Read more about a money class from Martin Fowler or this TSS discussion. Anders writes about Money in .NET.
Rant off. Thanks for not using double anymore.
If you liked this post, subscribe to my free full RSS feed.
Filed under: Java
What do you mean with “Use a distribute algorithm to distribute money to severel slots without loosing cents.”? Could you please explain this further? Anyway thanks for the links, good discussions there.
January 11th, 2008