Skip to main content
Ms Tela
Participant
July 9, 2016
Answered

Negative numbers not working in calculation

  • July 9, 2016
  • 1 reply
  • 2244 views

I am new to form field calculations and have run into an issue I can't resolve. I have a form with calculations. There are four columns, (beginning balance, receipts, expenditures and ending balance) the problem happens when a negative number is entered for the beginning balance. My calculation should be beginning balance + receipts - expenditures = ending balance but if a negative beginning balance is ed and you tab to receipts, enter receipts, tab to expenditures, enter expenditures, tab to ending balance I get the error "The value entered does not match the format of the field'. If beginning balance is a positive number there isn't a problem. I can send someone a copy of the form if that will help resolve this. Any help or suggestions would be greatly appreciated.

I am using Acrobat 9 Pro

Here is a sample of my calculation for ending balance:

// Get first field value
var v1 = getField("Beginning Balance.7").value;

// Get second field value
var v2 = getField("Receipts7").value;

// Get first field value
var v3 = getField("Expenditures7").value;

// Set this field value equal to the difference
event.value = v1 + v2 - v3;

This topic has been closed for replies.
Correct answer gkaiseril

Computers do not perform calculations using decimal values but convert the decimal values to binary values, perform the computation in the binary base, and then convert back to the decimal values. Since for many decimal value there is not an exact conversion value a small error gets introduced into the values and calculation. Changing the format to "None" and expanding the field for the result, one sees a value of -1.8189894035458565e-12  or the value of -"0." followed by 12 zeros and then the value 1.8189894035458565. A very small value. If one rounds the value of the calculation (not the displayed format of the Format option) one can get the answer of $0.00. One can verify this result by changing the "Format" option to "None" and expanding the field to accommodate the longer value.

I would use the util.printf method to perform the rounding for the result of the calculation.

Your calculation script could be:

// Get first field value
var v1 = getField("Beginning Balance.7").value;

// Get second field value
var v2 = getField("Receipts7").value;

// Get first field value
var v3 = getField("Expenditures7").value;

// Set this field value equal to the difference
event.value = util.printf("%,101.10f", Number(v1) + Number(v2) - Number(v3));

This script rounds to 10 decimal paces assuring the result will be within the value that can be displayed using the Number format.

1 reply

Inspiring
July 9, 2016

Are any of the input fields to your calculation empty?

A null field in some processed is treated like zero while in others it is treated like a string or non-numeric value. The "+" operator performs additions if the values being processed are clearly numeric values, but concatenates or joins the values if any value is not a numeric value.

Your script needs to make sure all values are numeric values. You can use the "Number" constrictor to convert a null string to zero.

// Get first field value
var v1 = getField("Beginning Balance.7").value;

// Get second field value
var v2 = getField("Receipts7").value;

// Get first field value
var v3 = getField("Expenditures7").value;

// Set this field value equal to the difference
event.value = Nmber(v1) + Number(v2) - v3;

Note: only when using the "+" operator does one need to force null strings to a zero value. All other arithmetic operations only process numbers and automatically force null stings to the numeric zero.

There are other ways to force a null string to zero that include multiplying by  the Multiplicative Identity, 1.

Ms Tela
Ms TelaAuthor
Participant
July 9, 2016

Thank you for responding. No, all the fields have at least a zero in them. I have attached the form. If you look at line 8 for kitchen, the beginning balance is -7,236.13, receipts is 21,190.85. If I enter the expenditures of 13,954.72 I get the error "The value entered does not match the format of the field (EndingBalance7).

Hopefully I am not making this worse.

Tela Harbold, Dept. Guard

VFW Auxiliary Dept. of Florida

★ Honesty ★ Integrity ★ Commitment

try67
Community Expert
Community Expert
July 10, 2016

You can't attach files to a message in these forums directly. Upload it to something like Dropbox or Google Drive and then post the link to it here.