Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Convert Numbers to Words in Acrobat Forms

New Here ,
May 11, 2023 May 11, 2023

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

 

TOPICS
JavaScript , PDF forms
3.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 12, 2023 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.

 

Capture_2305121041.png


Acrobate du PDF, InDesigner et Photoshopographe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 13, 2023 Jun 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 14, 2023 Jun 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 14, 2023 Nov 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". 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 14, 2023 Nov 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 ";

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 15, 2023 Nov 15, 2023

Perfect, thank you for the help!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 18, 2024 Jun 18, 2024
LATEST
!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;}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines