I’m adding multiple percentage fields in a form that should total 100% but it’s not working
1 Answers
Answer by Anonymous ✅ (Accepted)
This is due to a rounding issue with floating point numbers in Javascript.
FBA stores percentages as a decimal, so 50% = 0.5
In Javascript, adding 0.7 + 0.2 + 0.1 = 0.999999999 instead of 1
As such you need to round as follows…
Math.round(<<TotalPercent>> * 100) / 100
This assumes you’re dealing with percentages to 0dp e.g. 50%
If you’re dealing with higher precision e.g. 1dp or 50.5% then you need to implement as follows…
Math.round(<<TotalPercent>> * 1000) / 1000