Skip to main content
Samchaaya
Participant
November 12, 2016
Answered

ConvertToWords

  • November 12, 2016
  • 2 replies
  • 1373 views

Hello

I'm trying to convert numbers to string text, but having some difficulty doing it.

Found this string below on the web, sometimes it works, but most times it doesn't

I.e. - $10,000 change to --- Ten Thousand Dollars and Zero Cents

I have a very simple pdf file, with 2 fields, 1st is the numeric value and 2nd is the text field

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

or ive used

var f = this.getField("Number");event.value = ConvertToWords(f.value);

Any idea why its not reliable???

Tony

This topic has been closed for replies.
Correct answer George_Johnson

1. Select: Tools > JavaScript

2. Select "Document JavaScripts" from the toolbar that appears at the top

3. Give the new document JavaScript a name, such as "convert".

4. Click the Add button

5. In the window that appears, delete all of the code that it provides by default (an empty function) and enter the code for the ConvertToWords function and any other that it depends on.

Document-level JavaScripts are executed when the document is opened, so the code placed there will be available for use in the document thereafter.

2 replies

Inspiring
November 12, 2016

What does it mean "work reliably"?

Do you get any error messages when add the script to a field?

If so, what message?

Do you get any JavaScript console errors?

Use the "Ctrl" and "J" key combination to open the console.

If there is an issue with certain values or an empty field?

The Custom JavaScript for the Words field:

event.value = "";
if(this.getField("Number").valueAsString != "")
{
event.value = ConvertToWords(this.getField("Number").valueAsString);
}

Then the following script can be placed above the provided code or added as Document Level Script:

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 ";
if(num > -1 || num < 1) cWords = "No " + cWords;
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;
} // end ConvertToWords function;

Samchaaya
SamchaayaAuthor
Participant
November 12, 2016

Thanks gkaiseril

What I mean, this """event.value = ConvertToWords(this.getField("Number").value);""" worked the first time I did it, but then it stopped working

I'll try script now

Thanks

Inspiring
November 12, 2016

In order for it to work, the ConvertToWords function would have to be available. It is not a core JavaScript method, si it would have to be included in the document (e.g., in a document-level JavaScript) or in a folder-level JavaScript.

Samchaaya
SamchaayaAuthor
Participant
November 12, 2016

Thanks George

How do I include ConvertToWords as a document-level JavaScript in my PDF file

Thanks again

George_JohnsonCorrect answer
Inspiring
November 12, 2016

1. Select: Tools > JavaScript

2. Select "Document JavaScripts" from the toolbar that appears at the top

3. Give the new document JavaScript a name, such as "convert".

4. Click the Add button

5. In the window that appears, delete all of the code that it provides by default (an empty function) and enter the code for the ConvertToWords function and any other that it depends on.

Document-level JavaScripts are executed when the document is opened, so the code placed there will be available for use in the document thereafter.