Solved

Numeric value to words

Posted on 2004-09-04
14
357 Views
Last Modified: 2008-03-10
I have a adobe acrobat (6.0) form with a field that I only takes in currency values (curSalesPrice).  I would like an additional field (curSalesText) to display the numeric value of curSalesPrice in words.  For example, curSalesPrice has $52.00 curSalesText should read Fifty two dollars.

Your help is greatly appreciated.

Thanks!
0
Comment
Question by:chare1
  • 7
  • 7
14 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 11981669
This is actually not an Acrobat specific problem: You need a JavaScript routine that can convert numbers into English language strings. One example can be found on this page: http://www.merlyn.demon.co.uk/js-maths.htm#Cash
Look at the source (the source for the routine is near the end of the document). All you need to do is port this to Acrobat JavaScript.

I'll give it a try myself.
0
 
LVL 1

Author Comment

by:chare1
ID: 11981692
I tried using NumToString, unfortunately I don't believe this function is available in acrobat.  Also, the script on that page repeatedly mentions tries, in this instance what are they using tries for?
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 11981799
The "Tries" button is not important for you: It just loops through an array of values that are then copied to the input field (so that you don't have to make up your own test cases).

Here is what you have to do (I don't copy the script here because I don't want to create any copyright problems).

Create a document level JavaScript and name it "convertToEnglish". Add the following code for this function (it takes one parameter):

function convertToEnglish(inp)
{
   return TextCash(inp, 0);
}

Then copy the following items from the JavaScript from the web page I quoted:

Arrays: Affix, Name and Namety
Functions: Small and TextCash

Then, on your form just call convertToEnglish() with the value that you want to convert. E.g. if you have two fields, one with the numeric value, the second one with the string, use something like this:

var f1 = this.getField("Text1");
var f2 = this.getField("Text2");

f2.value = convertToEnglish(f1.value);


0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 1

Author Comment

by:chare1
ID: 11981882
So function Setup is not needed even though it contains the "names" of the amounts?  Also, for the document level javascript, I am assuming it is just a file named convertToEnglish.js.  What directory should it be placed in?  The same as that of the form?
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 11981899
It's not needed, because you copy the array declarations into the global part of your document level script.

A document level script is _NOT_ a file: It's embedded in the PDF document. Go to "Advanced>JavaScript>Document JavaScripts..." and type your new name and click on the "Add" button.
0
 
LVL 1

Author Comment

by:chare1
ID: 11981946
It still did not work.  I placed the script into the document javascripts and the text is still not appearing.  Should I post the exact code ?
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 11982146
Do you get any error messages in the JavaScript console/debugger (you can display it with Ctrl-J)?

0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 11982147
It's up to you if you want to post the code.
0
 
LVL 1

Author Comment

by:chare1
ID: 11982182
I don't get any error message.  Here is what I have under the document javascript:

function convertToEnglish()
{
 return TextCash(inp, 0);
}

Affix = new Array('units',
    'thousand', 'million', 'milliard', 'Tera', 'Peta', 'Exa')
  Name = new Array
    ('zero', 'one', 'two', 'three', 'four', 'five', 'six',
    'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve',
    'thirteen', 'fourteen', 'fifteen',  'sixteen', 'seventeen',
    'eighteen', 'nineteen')
  Namety = new Array('twenty', 'thirty', 'forty',
    'fifty', 'sixty', 'seventy', 'eighty', 'ninety')

function Small(TC, J, K) {
  if (J==0) return TC
  if (J>999) return ' Internal ERROR: J = ' + J + ' (>999)'
  var S = TC
  if (J>99) { S += Name[Math.floor(J/100)] + ' hundred ' ; J %= 100
    if (J>0) S += 'and '
    }
    else if ((S>'') && (J>0) && (K==0)) S += 'AND '
  if (J>19) { S += Namety[Math.floor(J/10)-2] ; J %= 10 ;
    S += ( J>0 ? '-' : ' ') }
  if (J>0) S += Name[J] + ' '
  if (K>0) S += Affix[K] + ' '
  return S }

function TextCash(L, K) {
  if (L==0) return (K>0 ? '' : 'NIL ')
  return Small(TextCash(Math.floor(L/1000), K+1), L%1000, K) }

Here is what I have in the field (under mouse up, run a javascript):

var f1 = this.getField("curBldrDeposit");
var f2 = this.getField("txtPriceWritten");

f2.value = convertToEnglish(f1.value);
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 11982215
This is pretty much what I have (I reformatted the source code a little to make it easier to understand, and I added the ';' after each statement, but it should work as it is).

What type of field is your "mouse up" code associated with? If it's your text field, don't use the "mouse up", but the "on blur" event.

You can find out if your event handler is called at all by adding this to the event handler:

console.println("in mouse-up handler");

Then check the JavaScript console for this message (you can leave the console up while you work with the document).
0
 
LVL 1

Author Comment

by:chare1
ID: 11982230
The error I am receiving is inp is not defined.
0
 
LVL 1

Author Comment

by:chare1
ID: 11982290
I added var inp and now I receive an error stating line 33 has too much recursion.  Here is line 33:

return Small(TextCash(Math.floor(L/1000), K+1), L%1000, K) }
0
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 125 total points
ID: 11982369
Look at the function definition from my earlier comment:

function convertToEnglish(inp)
{
   return TextCash(inp, 0);
}

The "inp" variable is a function argument.
0
 
LVL 1

Author Comment

by:chare1
ID: 11984525
Thank you for all your help!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Outlook 2010 corrupting incoming PDF attachments 23 3,982
PDF Digital Signatures 3 98
merge adobe pdf files 14 107
Acrobat DC Installation w/out Local Admins 7 65
Acrobat’s JavaScript is a great tool to extend the application, or to automate recurring tasks. There are several ways a JavaScript can be added to the application or a document (e.g. folder level scripts, validation scripts, event handling scripts,…
This article is in response to a question here (http://www.experts-exchange.com/Other/URLs/Q_28283850.html) at Experts Exchange. The Original Poster has a scanned signature and wants to make the background transparent so that the signature may be pl…
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question