Search code examples
javascriptpdfacrobatcalculated-field

custom calculation with results being in Currency format


I am making a PDF with a custom calculation script and I want to get the product of 2 fields with the results in the format of currency and for the life of me I cant figure it out. I am relatively new to a lot of this.

Here is the code I currently have:

var QtyRow1 = (this.getField( "QtyRow1").value); // the value of QtyRow1;
var CostRow1 = (this.getField( "CostRow1").value); // the value of CostRow1;

var t1 = QtyRow1 * CostRow1; // the value all TotalRows;

if ( t1 < .01 ) {
     // t1 will remain blank if total of CostRow1 * QtyRow are less than 1;
    event.value = "";
}  else {
    // otherwise will calculate total of all TotalRows;        
    event.value = t1;     
}

Additionally here is a link to my PDF I am working on from my google drive. I am trying to take the Quantity and Cost of each row and so a total in the format of Currency from the product of the quantity and cost.


Solution

  • So my problem was I wasn't identifying t1 as a number format with in the JavaScript code so when I would go to the format tab under the form field properties and select that I wanted it to formatted to number and currency it wouldn't work cause it would give me an error saying the "t1" hasn't been properly formatted. So all I did was within the if statement made sure to tell JavaScript that t1 needed to be formatted to a number and did so by doing this: event.value = (Number(t1)) as to where before I just had event.value = t1. That fixed it and now everything works great.

    Here is a link to the new PDF with the new code in it: Click Here

    // the value of form field QtyRow1;
    var QtyRow1 = (this.getField("QtyRow1").value);
    
    // the value of form field CostRow1;
    var CostRow1 = (this.getField("CostRow1").value);
    
    // the product of form fields QtyRow1 and CostRow1;
    var t1 = QtyRow1 * CostRow1; 
    
    // if statement for results
      if( t1 < .01 )
    {
    // t1 will remain blank if total of form fields CostRow1 * QtyRow are less than .01;
      event.value = ""; 
    
      } else {
    
    // otherwise will caclculate the product of form fields QtyRow1 and CostRow1;
      event.value = (Number(t1));
    }