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.

Comments are closed.