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

Complex Calculation Treating Negative Numbers as Zero in PDF Form

Community Beginner ,
Mar 12, 2025 Mar 12, 2025

jeremy_1903_0-1741786822592.png

 

I need a javascript for Complex Calculation where Total + SAI + Pell = Need; but if SAI is a negative number, treat as zero. Can anyone please assist me? 

TOPICS
JavaScript , PDF , PDF forms
289
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
2 ACCEPTED SOLUTIONS
Community Expert ,
Mar 12, 2025 Mar 12, 2025

Hi @jeremy_1903 when looking for scripts to validate or calculate something in Acrobat I found that ChatGPT returns very useful code. Please check the code if it does exactly what you are looking for because it was generated.

 

For your example this would be:

 

// Function to safely get a numeric value from a field
function getFieldNumber(fieldName) {
    var field = this.getField(fieldName);
    return field && field.valueAsString !== "" ? Number(field.valueAsString) : 0;
}

// Get values from form fields
var pell = getFieldNumber("pell");
var sai = Math.max(0, getFieldNumber("sai")); // Ensure SAI is non-negative
var total = getFieldNumber("total");

// Calculate the sum
var need = pell + sai + total;

// Set the calculated value in the "need" field
this.getField("need").value = need;

 

See a screen capture of it working here:

calc.gif

 

 

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
Community Expert ,
Mar 12, 2025 Mar 12, 2025

Use this in "Need" field as custom calculation script:

var total = Number(this.getField("Total").valueAsString);
var sai = Number(this.getField("SAI").valueAsString);
var pell = Number(this.getField("Pell").valueAsString);

event.value = total + Math.max(0, sai) + pell;

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
Community Expert ,
Mar 12, 2025 Mar 12, 2025

Hi @jeremy_1903 when looking for scripts to validate or calculate something in Acrobat I found that ChatGPT returns very useful code. Please check the code if it does exactly what you are looking for because it was generated.

 

For your example this would be:

 

// Function to safely get a numeric value from a field
function getFieldNumber(fieldName) {
    var field = this.getField(fieldName);
    return field && field.valueAsString !== "" ? Number(field.valueAsString) : 0;
}

// Get values from form fields
var pell = getFieldNumber("pell");
var sai = Math.max(0, getFieldNumber("sai")); // Ensure SAI is non-negative
var total = getFieldNumber("total");

// Calculate the sum
var need = pell + sai + total;

// Set the calculated value in the "need" field
this.getField("need").value = need;

 

See a screen capture of it working here:

calc.gif

 

 

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
Community Beginner ,
Mar 12, 2025 Mar 12, 2025

I didn't even consider ChatGPT, thats a great idea for the future. Thank you so much for your help! 

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
Community Expert ,
Mar 12, 2025 Mar 12, 2025
LATEST

But here you can also see that Nesa’s code is much shorter and probably more efficient. ChatGPT can be used as a starting point but as you can see below it is great to have the assistance of a professional coder.

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
Community Expert ,
Mar 12, 2025 Mar 12, 2025

Use this in "Need" field as custom calculation script:

var total = Number(this.getField("Total").valueAsString);
var sai = Number(this.getField("SAI").valueAsString);
var pell = Number(this.getField("Pell").valueAsString);

event.value = total + Math.max(0, sai) + pell;
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
Community Beginner ,
Mar 12, 2025 Mar 12, 2025

Thanks, Nesa! 

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