Skip to main content
New Participant
May 11, 2023
Question

Convert Numbers to Words in Acrobat Forms

  • May 11, 2023
  • 4 replies
  • 3588 views

I was able to get this to work for forms using this javascript: Keep in mind that if the field is a number, you need to put the proper input name: Number or String. It will not calculate correctly if it is the wrong input.

 

var strNumber = Number(this.getField("Field1").value) + 
                Number(this.getField("Field2").value) ;

var nNumber = Number(strNumber);

event.value = ConvertToWords(nNumber);


function ConvertToHundreds(num) {    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"
    ];

    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;
}

 

This topic has been closed for replies.

4 replies

New Participant
June 18, 2024
!Edit the script to work with other currencies
!e.g. change from 'twenty-one cents' to 'point two one'

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","Quintillion","Sextillion","Septillion"];
var cWords = (num >= 1 && num < 2) ? "" : "";
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 += "Point " + ConvertToHundreds(num/10) + " " + ConvertToHundreds(num%10);
else
cWords += "only"; 
return cWords;}

 

New Participant
November 14, 2023

Thank you this is very helpful. I'm trying to figure out how to omit "dollars" text, when there are only cents. For example, $0.90 should read as "Ninety Cents", but currently reads as "Dollars and Ninety Cents". 

try67
Adobe Expert
November 14, 2023

Replace this line of code:

 

var cWords = (num >= 1 && num < 2) ? "Dollar and " : "Dollars and ";

 

With:

 

   var cWords = "";
   if (num>=1 && num<2) 
	   cWords = "Dollar and ";
   else if (num>=2) 
	   cWords = "Dollars and ";

 

New Participant
November 15, 2023

Perfect, thank you for the help!

JR Boulay
Adobe Expert
June 14, 2023

"great example file provided with Acrobat 5" : this mean that it's not "my" code.

 

"the word "and" takes the place of a decimal"

What do you expect instead?

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Adobe Expert
May 12, 2023

You don't need to reinvent the wheel, download this great example file provided with Acrobat 5:

https://acrobat.adobe.com/link/track?uri=urn:aaid:scds:US:1b3599dd-a5db-4070-8d8c-2408b0f6f0f0

 

All you need is inside.

 

Acrobate du PDF, InDesigner et Photoshopographe
New Participant
June 13, 2023

When writing words, the word "and" takes the place of a decimal. This check would be written incorrectly using your code. Additionally, I am trying to figure out how to put in the comma to separate the millions and thousands text without adding it after "hundred" as well. Can you update this code or provide a work-around?