Copy link to clipboard
Copied
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; }
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
"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?
Copy link to clipboard
Copied
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".
Copy link to clipboard
Copied
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 ";
Copy link to clipboard
Copied
Perfect, thank you for the help!
Copy link to clipboard
Copied
Find more inspiration, events, and resources on the new Adobe Community
Explore Now