6 Replies Latest reply on Oct 20, 2014 8:29 PM by thamgrace

    javascript that can convert numeric values to words like

    chardmont007 Level 1

      Can anyone help me in this matter. I want to make automatic conversion of number to word like

      ex. 300 = Tree Hundred, in different box.

       

      Thanks in Advance

        • 1. Re: javascript that can convert numeric values to words like
          George_Johnson MVP & Adobe Community Professional

          This is a common programming exercise, so you should be able to find a lot of code out there that you can adapt. Google "JavaScript number to words".

          • 2. Re: javascript that can convert numeric values to words like
            chardmont007 Level 1

            I find some sample But I don't know how to execute, I new in scripting.

             

            var names = [{"0":"zero","1":"one","2":"two","3":"three","4":"four","5":"five","6":"six","7":"seven","8":"eight","9":"nine" },{"0":"ten","1":"eleven","2":"twelve","3":"thirteen","4":"fourteen","5":"fifteen","6":"sixteen","7":"seventeen","8":"eighteen","9":"nineteen"},{"2":"twenty","3":"thirty","4":"forty","5":"fifty","6":"sixty","7":"seventy","8":"eighty","9":"ninety"},["","thousand","million","billion","trillion","quadrillion","quintillion","sextillion","septillion","octillion","nonillion","decillion","undecillion","duodecillion","tredecillion","quattuordecillion", "quindecillion","sexdecillion","septdecillion","octdecillion","novemdecillion","vigintillion"]];
            var to_words = function(s, n){  // (Aug. 8, 2010) This function is probably horribly inefficient...
                 var ns = s.slice(0,3);          
                 return (ns.length < 1)?"":to_words(s.slice(3,s.length),n+1)+((ns.length>1)?((ns.length==3&&ns[2]!="0")?names[0][ns[2]]+" hundred "+((ns[1]=="1")?names[1][ns[0]]+" ":(ns[1]!="0")?names[2][ns[1]]+" "+((ns[0]!="0")?names[0][ns[0]]+" ":""):(ns[0]!="0"?names[0][ns[0]]+" ":"")):((ns[1]=="1")?names[1][ns[0]]+" ":(ns[1]!="0")?names[2][ns[1]]+" "+((ns[0]!="0")?names[0][ns[0]]+" ":""):(ns[0]!="0"?names[0][ns[0]]+" ":""))) + (((ns.length==3&&(ns[0]!="0"||ns[1]!="0"||ns[2]!="0"))||(ns.length==2&&(ns[0]!="0"||ns[1]!="0"))||(ns.length==1&&ns[0]!="0"))?"<span class='magnitude'>"+names[3][n]+"</span> ":""):((ns.length==1&&ns[0]!="0")?names[0][ns[0]]+" ":"") + (((ns.length==3&&(ns[0]!="0"||ns[1]!="0"||ns[2]!="0"))||(ns.length==2&&(ns[0]!="0"||ns[1]!="0"))||(ns.length==1&&ns[0]!="0"))?"<span class='magnitude'>"+names[3][n]+"</span> ":""));
            };
            document.id('input').addEvent('keyup', function(){  // some mootools magic
                document.id("output").set('html', to_words(this.value.replace(/[^0-9]/g, '').split('').reverse(), 0));
            }.bind(document.id('input'))).fireEvent('keyup');

             

            please help me to execute in my pdf form, (input tab different to output tab.

            • 3. Re: javascript that can convert numeric values to words like
              gkaiseril MVP & Adobe Community Professional

              That code is not for Acrobat JS use. There is a similar function in Acrobat JS. Adobe even provided a sample with the distribution disk of the full version of Acrobat.

               

              The provided code uses a document level function called 'ConvertToWord'. This function is then called a form field as needed.

               

              "Where is" Series, Entering Document Actions by Thom Parker

               

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

               

              In the appropriate form field, you can enter the following custom calculation script:

               

              var f = this.getField("Number");

              event.value = ConvertToWords(f.value);

               

              "Number" is the name of the field with numeric value to convert to words.

              • 4. Re: javascript that can convert numeric values to words like
                thamgrace

                hi,can anyone advise how to create the script in the document level script? where is this document level script located in the adobe pdf?

                fyi - I am using adobe acrobat XI version 11

                • 5. Re: javascript that can convert numeric values to words like
                  gkaiseril MVP & Adobe Community Professional

                  Unless you have Acrobat Professional, that can edit the document level scripts, this would be a lot of work to put into an FDF or console script to generate a document level script. If your form is one page, you can put the script in as an open document script.

                  • 6. Re: javascript that can convert numeric values to words like
                    thamgrace Level 1

                    hi Thanks.

                     

                    no wonder wouldn't find the document level script..

                     

                    anyway, I manage to do it at the specific field level by creating 2 fields. thanks for yr advice!