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

Script to convert numeric percentage into text?

raemay
Registered: Jun 16 2009
Posts: 4
Answered

i.e. [FIELD_APR]="4.25" convert to [FIELD_APR_TEXT]="Four and Twenty Five Hundredths"
 
Thank you for any ideas. The government is killing me with lending regulation changes!
 
-Rachel
 
I have this that converts currency into text:
 
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;
}
//
//
 
//
//Words:Calculate
//
/*********** belongs to: AcroForm:Words:Calculate ***********/
var f = this.getField("FIELD_APR");
 
event.value = ConvertToWords(f.value);
 
this.getField("FIELD_APR_TEXT").value = ConvertToWords(f.value);
//
//

Dimitri
Expert
Registered: Nov 1 2005
Posts: 1389
Hi raemay,

There have been several requests for this, so for a sample we developed for converting numbers to text we used a script found at http://www.easysurf.cc/cnvert18.htm. The code is HTML JavaScript but is fairly easy to convert to Acrobat JavaScript since it is pure JavaScript. It is probably best to use it in a Document level script.

Hope this helps,

Dimitri
WindJack Solutions
Online Acrobat JavaScript Training & Resource Library
WindJack Solutions
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
Accepted Answer
You need to modify the long script to make the "Dollar" and "Dollars" into null strings and change the "Cents" to "Hundredths'.

You will have a problem if you try to use your 'FIELD_APR' in a calculation or set the format as a percentage, since Acrobat like Excel uses the decimal value of the percent and displays a special formatted string of the value multiplied by 100 and the "%" symbol.

For the custom calculation script for the "FIELD_APR_TEXT" you only need:

event.value = ConvertToWords(this.getField("FIELD_APR").value);

George Kaiser

raemay
Registered: Jun 16 2009
Posts: 4
I think I got it. Thanks, you guys are wonderful! I used a combination of both your replies.