Skip to main content
Participating Frequently
Mar 5, 2025 4:43 PM UTC
Question

How to return 0 in JavaScript

 

 

I use the javascript below to calculate the differance between 2 values. It works fine unless the value is 0, then it displays nothing. Is it posible to make it display 0 if the 2 values are the same?

// Get the values from the textboxes
var value1 = parseFloat(this.getField("RealEstate_Loan_1").value) || 0;
var value2 = parseFloat(this.getField("RealEstate_MarketValue_1").value) || 0;

// Perform the subtraction
var result =  value2 - value1;

// Set the result in another field (e.g., ResultBox)
this.getField("RealEstate_NetValue_1").value = result.toFixed(2);

8 replies

PDF Automation Station
Community Expert
Community Expert
Mar 5, 2025 5:10 PM UTC

Enter the following Simplified Field notation in RealEstate_NetValue_1 and format the field as a number with 2 decimal places:

RealEstate_MarkeValue_1 - RealEstate_Loan_1

Translate
ordnance1Author
Participating Frequently
Mar 5, 2025 5:57 PM UTC

I altered the code to, but if the 2 textboxes are the same (say $10.00 then $10.00 - $10..00 should return $0.00) but textbox RealEstate_NetValue_1 displays nothing. The textrbox is formated as number with 2 decimal places.

 

var value1 = this.getField("RealEstate_Loan_1").value; 
var value2 = this.getField("RealEstate_MarketValue_1").value; 
event.value = value1 - value2; // Subtract the values

 

 

Translate
PDF Automation Station
Community Expert
Community Expert
Mar 5, 2025 6:19 PM UTC

It diplays 0.00 for me.  Can you upload your form?

Translate
Thom Parker
Community Expert
Community Expert
Mar 5, 2025 4:51 PM UTC

Also, where is this script placed?   If it's a caculation for "RealEstate_NetValue_1", then it should be a custom calculation on that field, and it should be written slightly differently. 

 

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
ordnance1Author
Participating Frequently
Mar 5, 2025 5:06 PM UTC

The Calculation is attached to textbox NetValue.  Format for textbox NetValue is set to Number, style 1,234.56 and currency is set to none.

Translate
PDF Automation Station
Community Expert
Community Expert
Mar 5, 2025 5:11 PM UTC

You have to change this.getField("RealEstate_NetValue_1").value to event.value and remove .toFixed(2)

Translate
Thom Parker
Community Expert
Community Expert
Mar 5, 2025 4:50 PM UTC

Is there formatting on the field?  The formatting controls what is displayed.

 

 

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often