Skip to main content
Known Participant
October 22, 2024
Question

Calculate Quotient After Determining Min

  • October 22, 2024
  • 3 replies
  • 879 views

I'm looking to calculate the minimum between two fields and then divide the minimum by an additional field to get a percentage. I'm not sure of the javascript that's needed (tried altering one from another post but it's giving an error message). For reference, I'm trying to calculate the minimum of "PurchasePrice" and "AsIsValue" and then divide the "InitialAdvance" by the minimum.

 

The typical formula would be: InitialAdvance / Min(PurchasePrice,AsIsValue)

 

Let me know if anyone can help!

This topic has been closed for replies.

3 replies

Thom Parker
Community Expert
Community Expert
October 22, 2024

See this article for basic info on writing a calculation script in Acrobat:

https://www.pdfscripting.com/public/How-to-Write-a-Basic-PDF-Calculation-Script.cfm

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
November 3, 2024

The formula listed above didn't work. ChatGPT told me to enter the following formula but it's giving me an error saying "SyntaxError: illegal character 1: at line 2"

 

Formula looks like this:

 

```javascript
var initialAdvance = this.getField("InitialAdvance").value;
var purchasePrice = this.getField("PurchasePrice").value;
var asIsValue = this.getField("AsIsValue").value;
var minValue;

if (!isNaN(purchasePrice) && !isNaN(asIsValue)) {
minValue = Math.min(purchasePrice, asIsValue);
} else {
event.value = "Error";

PDF Automation Station
Community Expert
Community Expert
November 3, 2024

Remove ```javascript

That is a heading that is not supposed to be in the script.

PDF Automation Station
Community Expert
Community Expert
October 22, 2024

Enter the following custom calculation script in the field where you want the result:

var mini=Math.min(this.getField("Purchase Price").value, this.getField("AsisValue").value);

if(mini==0)

{event.value=0}

else

{event.value=this.getField("InitialAdvance").value/mini}

Participant
October 22, 2024

You can solve this using JavaScript's Math.min() function to determine the minimum value between PurchasePrice and AsIsValue, and then divide InitialAdvance by that result. Here's the code:

 

let purchasePrice = /* Your PurchasePrice value */;
let asIsValue = /* Your AsIsValue value */;
let initialAdvance = /* Your InitialAdvance value */;

let minValue = Math.min(purchasePrice, asIsValue);
let quotient = initialAdvance / minValue;

console.log(quotient); // This will give you the percentage result

 


Make sure to replace the placeholders with actual values. This should resolve the issue and provide the correct quotient.

Bernd Alheit
Community Expert
Community Expert
October 22, 2024

This will not work in Acrobat.