In JavaScript/jQuery, arithmetic calculations use the built-in JavaScript Number type. A number is a floating point number with a limited precision of 64 bits, about 16 digits. In PHP, floating point numbers have limited precision as well, although it depends on the system. PHP typically uses the IEEE 754 double precision format.
Whatever the case, because of the limited precision of floating point numbers, rounding errors can occur during calculations. This is normal and unsurprising behavior. What's the fix?
Make sure you're working in the same precision and rounding for both PHP and jQuery. In some cases, you may have to cast the numbers as a specific number type like BigInt. I prefer jQuery because we can use the Math.js library. You can use Math.js in Formidable default value calculations. You also have the ability with jQuery to use built-in browser libraries for internationalizing number formats, which all modern browsers can do today.
A couple of resources I can recommend are https://formidableforms.com/knowledgebase/math-calculations/ and https://mathjs.org/
Please login or Register to submit your answer