Table XI

How to represent money in Ruby

Money. It makes the world go round.

It’s also rather important to many web applications, in that it’s what we ask our customers to give us in exchange for goods and services. And when we do, both us and our customers want those financial calculations to be very precise. Even tiny rounding errors add up, given time.

Unfortunately, the most direct way of representing money in a computer program, by using floating point numbers, known colloquially as decimals, isn’t precise enough and will cause problems on even simple calculations.

In this week’s XI to Eye video, we show how floating point numbers, such as 3.01, are represented internally, why that representation isn’t good enough for money and what a better workaround for managing money in Ruby looks like.