Why Use Integer In Finance
12/7/2022
I have implemented multiple payment processors in the past, and a rule I always follow is to use integers for all the currency calculations. The reason is simple. Floating-point arithmetic is not accurate.
And if you read the IEEE 754, it can be surprissing how innaccurate floating-point arithmetic is.
Floating point allows us to approximate all the values between a range due to its special encoding, but it cannot possibly represent all the numbers in that range and not even all the integers in that range.
Even though floating-point has its drawbacks, we may still want to use it to solve many computing problems where floating-point works and we accept its compromises.
However, this innaccuracies are a problem and as software developers we must understand how this encoding works and still produce correct results in our applications.
How real values are represented?
When we want to stor...