Skip to main content
Participating Frequently
March 5, 2025
Question

How to return 0 in JavaScript

  • March 5, 2025
  • 3 replies
  • 443 views

 

 

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);

3 replies

PDF Automation Station
Community Expert
Community Expert
March 5, 2025

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

ordnance1Author
Participating Frequently
March 5, 2025

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

 

 

PDF Automation Station
Community Expert
Community Expert
March 5, 2025

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

Thom Parker
Community Expert
Community Expert
March 5, 2025

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. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
ordnance1Author
Participating Frequently
March 5, 2025

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.

PDF Automation Station
Community Expert
Community Expert
March 5, 2025

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

Thom Parker
Community Expert
Community Expert
March 5, 2025

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

 

 

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