Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Form not calculating decimals correctly

New Here ,
Jun 22, 2017 Jun 22, 2017

I have a form that calculates how much a contract is, what change orders have been added, how much the customer has paid, how much is due this payment and how much balance remains.  All the fields are formatted as a 2 decimal.  For instance...

(n01) : 62,153.00

(n02) : 54,709.20

(n03) : 0.00

(n04) : 7443.80

The "simplified field notation" that calculates the balance due is n01 - n02 + n03 - n04

In the case of the example above, the balance (n05) should equal zero.  But instead, I get an error stating "The value entered does not match the format of the field [n05].  I eventually found out that it was calculating the total as 2.27848410531878E-12 or 0.00000000000227848410531878.  Why?  I figured out that I can write a custom calculation script and round the total to two decimal places, and that works.  But why is it calculating as a number with so many decimal places in the first place?  No matter how you look at it, 0.2 + 0.8 = 1.0 ... period.


Thanks.

TOPICS
PDF forms
2.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
LEGEND ,
Jun 22, 2017 Jun 22, 2017

Computers store numbers and perform calculation using the binary number system. Humans tend to use the decimal system. When decimal numbers are converted to binary values the results are not always the same exact number especially for decimal fractions. With the values you have selected your calculation in base 10 comes out to zero but in binary it is very close to zero but not exactly zero and the result in JavaScript is expressed using scientific notation. Scientific notation includes the symbol or letter "e" to denote exponentiation of the power of 10. This is not a valid symbol for the "Number" format".

To solve your issue, one needs to use the custom JavaScript calculation option and a user written function to either round each value used in the calculation or the final result before setting the field value.

A possible script with some debugging statements that can be removed:


function Round(nValue, nDec)
{
return Number(util.printf("%,101." + nDec + "f", nValue));
} // end Round function;

var n01 = this.getField("n01").value;
var n02 = this.getField("n02").value;
var n03 = this.getField("n03").value;
var n04 = this.getField("n04").value;

// some debugging statements;
console.show();
console.clear();
console.println("n01 - n02 = " + (n01 - n02));
console.println("Round(n01 - n02) = " + Round((n01 - n02), 2));
console.println("n01 - n02 + n03 - n04 = " + (n01 - n02 + n03 - n04));
console.println("Rounded = " + Round((n01 - n02 + n03 - n04), 2));
// end debugging statements;

event.value = Round((n01 - n02 + n03 - n04), 2);

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 22, 2017 Jun 22, 2017

Computers store numbers and perform calculation using the binary number system. Humans tend to use the decimal system. When decimal numbers are converted to binary values the results are not always the same exact number especially for decimal fractions. With the values you have selected your calculation in base 10 comes out to zero but in binary it is very close to zero but not exactly zero and the result in JavaScript is expressed using scientific notation. Scientific notation includes the symbol or letter "e" to denote exponentiation of the power of 10. This is not a valid symbol for the "Number" format".

To solve your issue, one needs to use the custom JavaScript calculation option and a user written function to either round each value used in the calculation or the final result before setting the field value.

A possible script with some debugging statements that can be removed:


function Round(nValue, nDec)
{
return Number(util.printf("%,101." + nDec + "f", nValue));
} // end Round function;

var n01 = this.getField("n01").value;
var n02 = this.getField("n02").value;
var n03 = this.getField("n03").value;
var n04 = this.getField("n04").value;

// some debugging statements;
console.show();
console.clear();
console.println("n01 - n02 = " + (n01 - n02));
console.println("Round(n01 - n02) = " + Round((n01 - n02), 2));
console.println("n01 - n02 + n03 - n04 = " + (n01 - n02 + n03 - n04));
console.println("Rounded = " + Round((n01 - n02 + n03 - n04), 2));
// end debugging statements;

event.value = Round((n01 - n02 + n03 - n04), 2);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 23, 2017 Jun 23, 2017
LATEST

Thanks for the reply.  That actually makes sense.  Thanks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines