Simple Calculation — 1 digit off

By: Todd Thornton | Asked: 02/07/2023
ForumsCategory: General questionsSimple Calculation — 1 digit off
Todd Thornton asked 2 years ago
I've created a simple calculation field but the resulting product is one digit off, but only in some cases. Here's the formula: [XXX] * [YYY] * (0.06 - [ZZZ]) = Product [20] * [10,000] * (0.06 - [0.02]) = $7,999 200,000 * 0.04 = $8,000 (of course this is the correct answer, but the form is kicking out $7,999)   Other permutations deliver the correct answer, for instance ... [20] * [10,000] * (0.06 - [0.03]) = $6,000 (correct!)   What's going on here? Any assistance greatly appreciated!   Thanks in advance!
1 Answers
Victor Font answered 2 years ago
I can't be sure, but this may have something to do with floating point arithmetic precision. I can't tell from the formula whether your calculation is being executed by jQuery or PHP, but both of them perform math calculations using floating point arithmetic and the both have different levels of precision.
Victor Font replied 2 years ago

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/

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right