14 Replies Latest reply on Mar 31, 2008 9:36 AM by (JasonSWalters)

    Convert a field with Numbers entered to text

    Level 1
      I'm trying to convert a dollars field to Pay field.

      Example.
      Dollars: 150.00
      I want it converted to...
      Pay: One hundred fifty dollars

      My fields are as said Dollars(numerals) and Pay(text). This is the code I have gotten so far that I found on the internet. But I'm not recieving my output into the Pay field. Anyone know why its not working?

      [code]
      //1st Script
      function outString(input) {
      var words2="";
      flag="false";
      var num2=null;
      set=new Array();
      typearr=new Array(" thousand, "," million, "," billion, "," trillion, "," quadrillion, ");
      arr=new Array();
      arr[2]=new Array("","one hundred","two hundred","three hundred","four hundred","five hundred","six hundred","seven hundred","eight hundred","nine hundred");
      arr[1]=new Array("","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety");
      arr[0]=new Array("","one","two","three","four","five","six","seven","eight","nine");
      teens=new Array("eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen", "nineteen")
      count=0;
      x=0;
      y=0;
      var num2=input;
      numarr=num2.split(",");
      num2=numarr.join("");
      if (parseFloat(num2)==0) {words2="zero";}
      else {
      while (num2.charAt(0)=="0") {
      num2=num2.substr(1);
      }
      set[x]=new Array();
      for (i=1;i<=num2.length;i++) {
      if (y>2) {
      y=0;
      x++;
      set[x]=new Array();
      }
      set[x][y]=num2.charAt(num2.length-i);
      y++;
      }
      for (j=x;j>=0;j--) {
      for (k=set[j].length-1;k>=0;k--) {
      if (k==1&&parseInt(set[j][k])==1&&parseInt(set[j][0])>0) {
      words2+=teens[parseInt(set[j][0])-1];
      k=0;
      } else {
      words2+=arr[k][parseInt(set[j][k])];
      }
      if (k==2) {
      if (parseFloat(set[j][k])!=0&&parseFloat(set[j][1]+set[j][0])!=0) {
      words2+=" and ";
      }
      if (j==0&&parseInt(set[j][k])==0) {
      if (words2.substr(words2.length-2,2)==", ") {
      words2=words2.substr(0,words2.length-2);
      }
      if (parseFloat(set[j][1]+set[j][0])!=0) {
      words2+=" and ";
      }
      }
      }
      if (k==1) {
      if (parseInt(set[j][0])!=0&&parseInt(set[j][k])>1) {words2+="-";}
      }
      if (k==0&&j>0) {
      if (parseFloat(set[j].join(""))>0) {
      words2+=typearr[j-1];
      }
      }
      }
      }
      }
      return words2;
      }

      var BA = this.getField("Dollars");
      var BAVal = BA.value;
      //event.value = outString(BAVal);
      //this.getField("Pay").value
      console.println(outString(BA.valueAsString));
      console.println(outString(BAVal));
      [/code]
        • 1. Re: Convert a field with Numbers entered to text
          gkaiseril MVP & Adobe Community Professional
          Do you get any error message on the JavaScript console?

          I use the following script provided by Acrobat in the sample forms.

          // Document level JavaScript and function
          // document level variables
          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);
          }
          if (cWords == "Dollars and ") cWords = "Zero " + cWords;
          num = Math.round(num * 100) % 100;
          if (num > 0)
          cWords += ConvertToHundreds(num) + " Cents";
          else
          cWords += "Zero Cents";

          return cWords;
          }
          // end doucment level script and function

          // custom calculation script for "Pay" text field no format applied
          var f = this.getField("Dollars");
          event.value = ConvertToWords(f.value);
          // end custom calculation script for "Pay" field
          • 2. Re: Convert a field with Numbers entered to text
            Level 1
            I just tried putting in exactly what you posted last and it gave me an error saying The value entered does not match the format of the field dollars. And I am doing this form in acrobat not in live cycle or any of the other designers.
            • 3. Re: Convert a field with Numbers entered to text
              (Aandi_Inston) Level 1
              Have you attached this code to the dollars field or the pay field?

              Aandi Inston
              • 4. Re: Convert a field with Numbers entered to text
                Level 1
                To the Dollars field under Custom Calculation Script
                • 5. Re: Convert a field with Numbers entered to text
                  (Aandi_Inston) Level 1
                  That's the problem. You want to calculate a text value for pay. The
                  code ends by setting event.value, i.e. the value of the current field.

                  Aandi Inston
                  • 6. Re: Convert a field with Numbers entered to text
                    Level 1
                    I feel stupid works great now. One last question it says and Zero Cents on the end is there a way to make it like a check and do and 25/100 or 00/100... If so what part do I need to take out?
                    • 7. Re: Convert a field with Numbers entered to text
                      gkaiseril MVP & Adobe Community Professional
                      Replace the document level function code starting at "function ConvertToWords(num)" code with:

                      function ConvertToWords(num)
                      {
                      var aUnits = new Array("Thousand", "Million", "Billion", "Trillion", "Quadrillion");
                      var cWords = (num >= 1 && num < 2) ? "Dollar and " : "Dollars and ";
                      var nLeft = Math.floor(num);
                      // process whole dollars
                      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);
                      }
                      // add "Zero" or ohter literal for aero dollars
                      if (cWords == "Dollars and ") cWords = "Zero " + cWords;

                      // process cents
                      num = Math.round(num * 100) % 100;

                      // add ##/100 to dollar words for cents
                      cWords = cWords + util.printf("%,102d", num) + "/100";

                      /*
                      // cents splled out to dollar words
                      if (num > 0)
                      cWords += ConvertToHundreds(num) + " Cents";
                      else
                      cWords += "Zero Cents";
                      */

                      return cWords;
                      }
                      • 8. Re: Convert a field with Numbers entered to text
                        Level 1
                        It gave me the error comment not terminated
                        • 9. Re: Convert a field with Numbers entered to text
                          gkaiseril MVP & Adobe Community Professional
                          The dislayed code should work now, the "*" was changed to a bullet.
                          • 10. Re: Convert a field with Numbers entered to text
                            Level 1
                            Yeh I figured that why it was giving that error, I took a java class a while back. But I don't know javascript really. Now it is giving me a new error I have no clue about, It says "return not in function" and it is talking about the very last return. Line 72 at 73
                            • 11. Re: Convert a field with Numbers entered to text
                              gkaiseril MVP & Adobe Community Professional
                              It get dificult to cut and paste JavaScript code into an HTML page.

                              function ConvertToWords(num) {
                              var aUnits = new Array("Thousand", "Million", "Billion", "Trillion", "Quadrillion");

                              var cWords = (num >= 1 && num < 2) ? "Dollar and " : "Dollars and ";
                              var nLeft = Math.floor(num);

                              // process whole dollars

                              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);
                              }
                              // add "Zero" or ohter literal for aero dollars

                              if (cWords == "Dollars and ") cWords = "Zero " + cWords;

                              // process cents

                              num = Math.round(num * 100) % 100;

                              // add ##/100 to dollar words for cents

                              cWords = cWords + util.printf("%,102d", num) + "/100";

                              /*
                              // cents splled out to dollar words
                              if (num > 0)
                              cWords += ConvertToHundreds(num) + " Cents";
                              else
                              cWords += "Zero Cents";
                              */

                              return cWords;
                              }
                              • 12. Re: Convert a field with Numbers entered to text
                                Level 1
                                Man, this form is starting to become a nightmare... every time we try something it gets worse.. I know it has to be close... But now its not giving me any output at all... However, it didnt give me anymore warnings
                                • 13. Re: Convert a field with Numbers entered to text
                                  (Aandi_Inston) Level 1
                                  You really do need to examine and understand each line. When you copy
                                  and paste only, it could have been wrong in the first place or mangled
                                  by the forum software. Try going through it like by line and working
                                  out exactly what it is supposed to do. Don't assume things work
                                  because they look about right. Check carefully that the { and } are in
                                  the right place. Longer process, but more valuable in the long term,
                                  I think.

                                  Aandi Inston
                                  • 14. Re: Convert a field with Numbers entered to text
                                    Level 1
                                    Well I got it working perfect. Thank you everyone who helped I am hopefully done with this for a good while. Jason.