I have a calculation entered as Simplified Field Notation for a field on an interactive pdf where a user can enter values and the form auto-calculates other fields based on what is entered.
Simplified, the calculation is a*(100-b)/100. Given a=0.54 and b=15, the field yields 0.459 which is shown to 2 decimal places on the pdf as 0.46.
I need to round that answer to 2 decimal places so that another calculation pulls 0.46 from this field instead of 0.459. In Excel or elsewhere, I would round 0.459 to 2 decimal places. Or I would multiply 0.459 times 100, truncate the decimal, then divide by 100. Either way, the next calculation needs to think the calculated value in that field is 0.46, not 0.459.
Is there a javascript command I can add to the calculation to achieve this? If it helps any, the other calculation is set up as "Value is the product (x) of the following fields:" with the above field listed as one of the factors (which I would guess limits where something can be done).
Thank you.
JavaScript has a 'round' method for the 'Math' object, but this does not always round correctly due to the way in which floating point numbers are stored and computed by the computer.
Also it is not a good idea to perform a division in the 'simplified field notation' calculation unless you know that under all circumstances you know that the divisor will not be zero or null.The rounding function can be performed by use of either the 'Math.round()' or 'util.printf()' methods.
Using the 'Math.round()' method one could write the following funciton:
function Round(fNumber, fDec) {
/*
Purpose: Round a number to a given number of decimal places
Parameters:
fNumber - number to be rounded
fDec - number of decimal places to round to.
If omitted zero decimal places is assumed.
Function will work just like Math.round method
Returns number rounded to given number of decimal places
*/
// check to see fDec passed
if(typeof fDec == 'undefined') {
// fDec parameter does not exist
fDec = 0; // force value to zero
}
fDec = Math.floor(fDec); // only allow whole numbers
// compute value for decimal adjustment
var fAdjust = Math.pow(10, fDec);
// compute and return rounded number
return Math.round(fNumber * fAdjust) / fAdjust;
} // end of Round funciton
Using the 'util.printf()' method, which does not appear to have a problem, one can use the following funciton;
function Round(nValue, nDec)
{
return Math.round(Number(nValue) * Math.pow(10, Number(nDec))) / Math.pow(10, Number(nDec));
// return Number(util.printffunctionnumber( "%,0 ." + Numbeadjustr(nDec)functionfunction + "f", Number(nValue))) ;
}
Using a function to perform the rounding will allow one to reuse the code in more than one calculation or other JavaScript application.
If you want to use a calculation option other then the 'custom JavaScript' you can use the 'custom javascript' on the 'Validation' tab to perform the rounding.
if (event.valueAsString != "") event.value = Round(event.value,2);
If you want to use the 'custom JavaScript calculation' with the Round function:
var a = Number(this.getField('a').value);
var b = Number(this.getField('b').value);
event.value = Round( (a * (100 - b) / 100), 2);
If you do not want to use the Round function:
// get variables as number
var a = Number(this.getField('a').value);
var b = Number(this.getField('b').value);
// compute
var nValue = a * (100 - b) / 100;
// format for 2 decimals
var cRound = "%,0 ." + 2 + "f";
// round the computed value
event.value = util.printf( cRound, nValue);
George Kaiser