These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Slow Running Java Script

Lady Di
Registered: Feb 18 2009
Posts: 86

I have several Document Level Java Scripts on a form that I am creating. One of these scripts takes a number from a field and translates it into words (in other words 10 - ten). The numeric field that it is based on is a calculated field. The field calculates quickly, but the script takes forever to update the words. How can I make this move smoothly, so that when the cacluation changes, the words promptly change?

My Product Information:
Acrobat Pro 8.0, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
You can start by posting the script so we can see if it can be made more efficient.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Lady Di
Registered: Feb 18 2009
Posts: 86
Here is the Document Level Script:

aTens = [ "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
aOnes = [ "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
"Nineteen" ];

function ConvertToHundreds(num)
{
var cNum, nNum;
var cWords = "";

num %= 1000;
if (num > 99) {
/* Hundreds. */
cNum = String(num);
nNum = Number(cNum.charAt(0));
cWords += aOnes[nNum] + " Hundred";
num %= 100;
if (num > 0)
cWords += " and "
}



if (num > 19) {
/* Tens. */
cNum = String(num);
nNum = Number(cNum.charAt(0));
cWords += aTens[nNum - 2];
num %= 10;
if (num > 0)
cWords += "-";
}



if (num > 0) {
/* Ones and teens. */
nNum = Math.floor(num);
cWords += aOnes[nNum];
}



return cWords;
}



function ConvertToWords(num)
{
var aUnits = [ "Thousand", "Million", "Billion", "Trillion", "Quadrillion" ];
var cWords = (num >= 1 && num < 2) ? "Dollar and " : "Dollars and ";
var nLeft = Math.floor(num);
for (var i = 0; nLeft > 0; i++) {
if (nLeft % 1000 > 0) {
if (i != 0)
cWords = ConvertToHundreds(nLeft) + " " + aUnits[i - 1] + " " + cWords;
else
cWords = ConvertToHundreds(nLeft) + " " + cWords;
}
nLeft = Math.floor(nLeft / 1000);
}
num = Math.round(num * 100) % 100;
if (num > 0)
cWords += ConvertToHundreds(num) + " Cents";
else
cWords += "Zero Cents";

return cWords;
}



Then the below script is placed on the custom calculation script of the text field:

var nValue=this.getField("Numerical").value;event.value=ConvertToWords(nValue);