Only round at the last possible moment

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the algorithms category.

Last Updated: 2024-11-21

I had to convert the taxation percentages given online from the government format (1.025% employer, 3.3% total) into a proportional component that worked with our legacy code. At first, I wrote the proportion of employer tax compared to the total as (1.025/3.3) as 0.31, chopping off a load of digits at the end.

This, however, was inaccurate, especially with larger figures. I should have used the full number from the calculation - 0.310606. The reason I didn't put this in initially was because I was rounding to 2 significant digits. But this rounding should only occur at the latest possible time.