5 Replies Latest reply on Nov 28, 2016 6:30 AM by Dzedimainas

    Convert text value to numeric value

    Dzedimainas

      Hi all,

       

      Is there a way to convert a text value of one text field to a numeric value in another text field? For example, if text field's "MonthText" value is "January", then the text field's "MonthNum" value is "01". I'm sure there is a way, but my knowledge is limited...

       

      Thanks

        • 1. Re: Convert text value to numeric value
          try67 MVP & Adobe Community Professional

          There are many ways this can be done... For example, you can use an array of literal objects, or two arrays, one with the name of the month and one with its number, etc.

          • 2. Re: Convert text value to numeric value
            Dzedimainas Level 1

            Thanks for your reply. Like I said, I don't have much experience with this, but as far as I understand, it would go something like this?:

             

            var MonthText = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",                                          "December"];

             

            var MonthNum = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];

             

            Now, how do I tie them? If.... ??

            • 3. Re: Convert text value to numeric value
              Test Screen Name Most Valuable Participant

              You'd step through the MonthText array looking at each value. If you find a match at a given index, take the item with the same index from MonthNum.

              • 4. Re: Convert text value to numeric value
                try67 MVP & Adobe Community Professional

                Yes, that's correct. So to tie them you need to get the index of the value in the first array and then use that some index in the second one.

                This could be the custom calculation script of the "MonthNum" field:

                 

                var monthTexts = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
                var monthNumbers = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];
                
                var monthText = this.getField("MonthText").valueAsString;
                var i = monthTexts.indexOf(monthText);
                if (i==-1) event.value = "";
                else event.value = monthNumbers[i];
                

                 

                In this case, though, since the second array is just numbers then you don't actually need it. You can use this instead (as the last line of the code):

                 

                else event.value = util.printf("%02d", (i+1));
                
                1 person found this helpful
                • 5. Re: Convert text value to numeric value
                  Dzedimainas Level 1

                  THANK YOU!!!! Exactly what I needed.

                   

                  Thanks again!!!